json string轉(zhuǎn)換為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(由于在應(yīng)用中沒有完全匹配的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);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容