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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容