详解json string转换为java bean及实例代码
作者:jacksu在简书 时间:2021-07-04 01:48:26
详解json string转换为java bean及实例代码
pom中添加如下两个库:
<dependency>
<groupId>org.codehaus.jackson </groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba </groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
<scope>provided</scope>
</dependency>
java bean的定义为:
package test.fastjson;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
//简单地忽略掉从JSON(由于在应用中没有完全匹配的POJO)中获得的所有“多余的”属性
@JsonIgnoreProperties(ignoreUnknown = true)
public class ESMetaDataInfoIndex
{
//改变某个成员属性所使用的JSON名称
@JsonProperty("tableStrategy")
private String tableStrategy = null;
@JsonProperty("indexName")
private String indexName = null;
@JsonProperty("topic")
private String topic = null;
@JsonProperty("nameSpace")
private String nameSpace = null;
@JsonProperty("extendAttr")
private Map<String, String> extendAttr = new HashMap<String, String>();
@JsonProperty("type")
private String type = null;
@JsonProperty("ttl")
private int ttl = 0;
@JsonProperty("splitCol")
private String splitCol = null;
/**
**/
public String getTableStrategy()
{
return tableStrategy;
}
public void setTableStrategy(String tableStrategy)
{
this.tableStrategy = tableStrategy;
}
/**
**/
public String getIndexName()
{
return indexName;
}
public void setIndexName(String indexName)
{
this.indexName = indexName;
}
/**
**/
public String getTopic()
{
return topic;
}
public void setTopic(String topic)
{
this.topic = topic;
}
/**
**/
public String getNameSpace()
{
return nameSpace;
}
public void setNameSpace(String nameSpace)
{
this.nameSpace = nameSpace;
}
/**
**/
public Map<String, String> getExtendAttr()
{
return extendAttr;
}
public void setExtendAttr(Map<String, String> extendAttr)
{
this.extendAttr = extendAttr;
}
/**
**/
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
/**
**/
public int getTtl()
{
return ttl;
}
public void setTtl(int ttl)
{
this.ttl = ttl;
}
/**
**/
public String getSplitCol()
{
return splitCol;
}
public void setSplitCol(String splitCol)
{
this.splitCol = splitCol;
}
}
测试用例为:
ESMetaDataInfoIndex dataInfo = JSON.parseObject(json.toJSONString(),ESMetaDataInfoIndex .class);
来源:http://www.jianshu.com/p/d4428c1b83fb
标签:json,string,java,bean
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring中bean的生命周期详解
2021-11-29 23:31:02
Java字符串查找的三种方式
2023-11-18 22:00:30
java开发之内部类的用法
2023-02-04 21:30:07
Java中方法的重写与成员变量的隐藏
2023-06-01 01:35:55
Java事件处理机制(自定义事件)实例详解
2023-10-28 21:30:50
Swing拆分窗格控件JSplitPane使用详解
2022-11-14 21:13:48
![](https://img.aspxhome.com/file/2023/0/63500_0s.jpg)
Java中值类型和引用类型详解
2023-11-29 00:44:51
![](https://img.aspxhome.com/file/2023/9/60559_0s.png)
java jdk1.8 使用stream流进行list 分组归类操作
2022-10-16 10:03:52
JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解
2023-11-14 00:27:55
![](https://img.aspxhome.com/file/2023/5/58985_0s.png)
springboot快速集成mybatis-plus的详细教程
2021-06-11 22:05:16
![](https://img.aspxhome.com/file/2023/4/73014_0s.png)
c#与js随机数生成方法
2023-12-14 12:30:36
Java经典面试题最全汇总208道(三)
2023-11-15 23:30:42
![](https://img.aspxhome.com/file/2023/6/59066_0s.jpg)
Java8函数式接口的基础学习教程
2023-12-16 12:57:59
java分页工具类的使用方法
2023-08-17 02:00:14
基于ThreadLocal 的用法及内存泄露(内存溢出)
2023-11-23 20:22:28
![](https://img.aspxhome.com/file/2023/9/66509_0s.png)
基于FLink实现实时安全检测的示例代码
2022-05-06 11:03:11
Java界面编程实现界面跳转
2023-11-12 00:56:45
![](https://img.aspxhome.com/file/2023/7/63847_0s.jpg)
springboot2.x使用Jsoup防XSS攻击的实现
2023-11-17 06:40:47
![](https://img.aspxhome.com/file/2023/3/58733_0s.png)
Java反射概念与使用实例代码
2022-02-24 23:55:40
java并发编程工具类JUC之ArrayBlockingQueue
2023-07-04 21:02:20