溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解json string轉(zhuǎn)換為java bean及實(shí)例代碼

發(fā)布時(shí)間:2020-10-24 12:42:56 來源:腳本之家 閱讀:144 作者:jacksu在簡(jiǎn)書 欄目:編程語言

詳解json string轉(zhuǎn)換為java bean及實(shí)例代碼

pom中添加如下兩個(gè)庫:

<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;

//簡(jiǎn)單地忽略掉從JSON(由于在應(yīng)用中沒有完全匹配的POJO)中獲得的所有“多余的”屬性
@JsonIgnoreProperties(ignoreUnknown = true)
public class ESMetaDataInfoIndex  
{
  //改變某個(gè)成員屬性所使用的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;
  }   
}

測(cè)試用例為:

ESMetaDataInfoIndex dataInfo = JSON.parseObject(json.toJSONString(),ESMetaDataInfoIndex .class);

以上就是json string轉(zhuǎn)換為java bean的實(shí)例,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI