详解json string转换为java bean及实例代码
程序员文章站
2023-12-19 09:30:46
详解json string转换为java bean及实例代码
pom中添加如下两个库:
详解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);
以上就是json string转换为java bean的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!