• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于json string如何转换成java bean的实例分析

java 搞代码 4年前 (2022-01-09) 12次浏览 已收录 0个评论

这篇文章主要介绍了详解json string转换为java bean及实例代码的相本文来源gao@dai!ma.com搞$代^码!网7关资料,这里提供实例代码帮助大家理解,需要的朋友可以参考下

详解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的实例分析的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于json string如何转换成java bean的实例分析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址