JSON.parseObject将字符串转为bean对象

JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。

String sb=

{"message":"success","store":false,"redirect":"","term":[{"index":0,"term":"华为"},{"index":1,"term":"手机"}],"central":{"index":1,"type":"pmc","value":"手机"},"res":[{"value":"华为","index":"0","type":"pmb"},{"value":"手机","index":"1","type":"pmc"}],"brand":{"brandError":false,"brandRelationHas":false,"familyBrand":false,"hasBrand":true,"luceneBrand":false,"pmbIndex":-1,"pureBrand":false},"relate":{"pmb":[],"cen":[{"extension":0,"extensionV2":"手机通讯","field":"groupIDCombination","id":"20006","similarity":0.9919,"value":"手机"}]},"info":[[{"extension":0,"extensionV2":"","field":"brand_Name","id":"0864","similarity":1.0,"value":"华为(HUAWEI)"},{"extension":0,"extensionV2":"手机通讯","field":"groupIDCombination","id":"20006","similarity":0.9919,"value":"手机"}]],"entities":[{"entities":[{"extension":0,"extensionV2":"","field":"brand_Name","id":"0864","similarity":1.0,"value":"华为(HUAWEI)"},{"extension":0,"extensionV2":"手机通讯","field":"groupIDCombination","id":"20006","similarity":0.9919,"value":"手机"}],"score":100.0}],"rec":[[{"extension":0,"extensionV2":"","field":"brand_Name","id":"0864","similarity":1.0,"value":"华为(HUAWEI)"}],[{"extension":0,"extensionV2":"手机通讯","field":"groupIDCombination","id":"20006","similarity":0.9919,"value":"手机"}],[]],"label":[[{"extension":0,"extensionV2":"","field":"brand_Name","id":"0864","similarity":1.0,"value":"华为(HUAWEI)"}],[{"extension":0,"extensionV2":"手机通讯","field":"groupIDCombination","id":"20006","similarity":0.9919,"value":"手机"}],[]]}



QueryResultBean queryResultBean = JSON.parseObject(sb.trim(), QueryResultBean.class);

将上面的json转为bean对象后可以直接获取

modelMap.put("term", queryResultBean.toTermStr());

 modelMap.put("info", queryResultBean.toInfoStr());


结果为:

{term=华为 手机 , info=brand_Name:华为(HUAWEI) groupIDCombination:手机}



关键在Bean中的写法,决定输出结果的格式:

1、QueryResultBean.java


public class QueryResultBean {
    private List term = new ArrayList();
    
    private List> info = new ArrayList>();
    


   


    public List getTerm() {
        return term;
    }


    public void setTerm(List term) {
        this.term = term;
    }


    public List> getInfo() {
        return info;
    }


    public void setInfo(List> info) {
        this.info = info;
    }


    public String toTermStr() {
        StringBuilder t = new StringBuilder();
        for (TermBean termBean : term) {
            t.append(termBean.getTerm()).append(" ");
        }
        return t.toString();
    }


    public String toInfoStr() {
        StringBuilder sBuilder = new StringBuilder();
        for (List list : info) {
            for (GeneralEntity infoBean : list) {
                sBuilder.append(infoBean.field).append(":").append(infoBean.getValue()).append(" ");
            }
            sBuilder.append("\r\n");
        }
        return sBuilder.toString();
    }


}



2、TermBean.java



public class TermBean {
    private Integer index;
    private String term;
    
    public TermBean() {
    }
    public TermBean(Integer index, String term) {
        this.index = index;
        this.term = term;
    }
    public Integer getIndex() {
        return index;
    }
    public void setIndex(Integer index) {
        this.index = index;
    }
    public String getTerm() {
        return term;
    }
    public void setTerm(String term) {
        this.term = term;
    }
}


3、通用实体GeneralEntity.java



import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Comparator;


/**
 * 通用实体
 */
public class GeneralEntity implements Serializable {


    private static final long serialVersionUID = 4020348283149544060L;


    public String id = "0";//实体id
    public String field = "";//具体领域
    public String value = "";//领域值
    public double similarity = 0;//相似度
    public int extension = 0;//扩展字段(int,精排黑名单等)
    public String extensionV2 = "";//扩展字段(String)


    public GeneralEntity() {
    }


    public GeneralEntity(String field, String value, int extension) {
        this.field = field;
        this.value = value;
        this.extension = extension;
    }


    public GeneralEntity(String id, String field, String value, double similarity, String extensionV2) {
        this.id = id;
        this.field = field;
        this.value = value;
        BigDecimal b = new BigDecimal(similarity);
        this.similarity = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        this.extensionV2 = extensionV2;
    }


    public GeneralEntity(String id, String field, String value, double similarity, int extension, String extensionV2) {
        this.id = id;
        this.field = field;
        this.value = value;
        BigDecimal b = new BigDecimal(similarity);
        this.similarity = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        this.extension = extension;
        this.extensionV2 = extensionV2;
    }


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getField() {
        return field;
    }


    public void setField(String field) {
        this.field = field;
    }


    public String getValue() {
        return value;
    }


    public void setValue(String value) {
        this.value = value;
    }


    public int getExtension() {
        return extension;
    }


    public void setExtension(int extension) {
        this.extension = extension;
    }


    public String getExtensionV2() {
        return extensionV2;
    }


    public void setExtensionV2(String extensionV2) {
        this.extensionV2 = extensionV2;
    }


    public double getSimilarity() {
        return similarity;
    }


    public void setSimilarity(double similarity) {
        BigDecimal b = new BigDecimal(similarity);
        this.similarity = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    public static class AzComparator implements Comparator {
        @Override
        public int compare(GeneralEntity a1, GeneralEntity a2) {
            return Double.compare(a2.similarity, a1.similarity);
        }
    }


    @Override
    public int hashCode() {
        final int prime = 100;
        int res = 1;
        res = prime * res + ((id == null) ? 0 : id.hashCode());
        res = prime * res + ((field == null) ? 0 : field.toLowerCase().hashCode());
        res = prime * res + ((value == null) ? 0 : value.hashCode());
        return res;
    }


    @Override
    public boolean equals(Object obj) {
        GeneralEntity entity;
        if (obj instanceof GeneralEntity) {
            entity = (GeneralEntity) obj;
        } else {
            return false;
        }
        return entity.id != null && entity.id.equals(this.id) && entity.field.equals(this.field) && entity.value.equals(this.value);
    }
}

你可能感兴趣的:(JAVA常用)