原创

java实现复杂json字符串转List集合获取对象属性的值

如何拿到datasetName的值?

 [{"msg":"存在低俗辱骂不合规","conclusion":"不合规","hits":[{"wordHitPositions":[{"positions":[[0,2]],"label":"500100","keyword":"草泥马"}],"probability":1.0,"datasetName":"百度默认文本反作弊库","words":["草泥马"],"modelHitPositions":[[0,2,0.9996]]}],"subType":5,"conclusionType":2,"type":12}]

假设如上述json字符串,直接上代码吧!

各个对象实体类,及实体与实体的关联关系

package com.jingxuan.prize;
import java.util.List;

public class JsonBean {

    private String msg;
    private String conclusion;
    private List<Hits> hits;
    private int subType;
    private int conclusionType;
    private int type;
    public void setMsg(String msg) {
         this.msg = msg;
     }
     public String getMsg() {
         return msg;
     }

    public void setConclusion(String conclusion) {
         this.conclusion = conclusion;
     }
     public String getConclusion() {
         return conclusion;
     }

    public void setHits(List<Hits> hits) {
         this.hits = hits;
     }
     public List<Hits> getHits() {
         return hits;
     }

    public void setSubType(int subType) {
         this.subType = subType;
     }
     public int getSubType() {
         return subType;
     }

    public void setConclusionType(int conclusionType) {
         this.conclusionType = conclusionType;
     }
     public int getConclusionType() {
         return conclusionType;
     }

    public void setType(int type) {
         this.type = type;
     }
     public int getType() {
         return type;
     }

}
import java.util.List;

public class WordHitPositions {

    private List positions;
    private String label;
    private String keyword;
    

    public void setLabel(String label) {
         this.label = label;
     }
     public String getLabel() {
         return label;
     }

    public List getPositions() {
		return positions;
	}
	public void setPositions(List positions) {
		this.positions = positions;
	}
	public void setKeyword(String keyword) {
         this.keyword = keyword;
     }
     public String getKeyword() {
         return keyword;
     }

}
import java.util.List;


public class Hits {

    private List<WordHitPositions> wordHitPositions;
    private int probability;
    private String datasetName;
    private List<String> words;
    private List<List> modelHitPositions;
    public void setWordHitPositions(List<WordHitPositions> wordHitPositions) {
         this.wordHitPositions = wordHitPositions;
     }
     public List<WordHitPositions> getWordHitPositions() {
         return wordHitPositions;
     }

    public void setProbability(int probability) {
         this.probability = probability;
     }
     public int getProbability() {
         return probability;
     }

    public void setDatasetName(String datasetName) {
         this.datasetName = datasetName;
     }
     public String getDatasetName() {
         return datasetName;
     }

    public void setWords(List<String> words) {
         this.words = words;
     }
     public List<String> getWords() {
         return words;
     }
	public List<List> getModelHitPositions() {
		return modelHitPositions;
	}
	public void setModelHitPositions(List<List> modelHitPositions) {
		this.modelHitPositions = modelHitPositions;
	}

}

测试类

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class BaiDuTests {
	public static void main(String[] args) {
		String strs = "[{\"msg\":\"存在低俗辱骂不合规\",\"conclusion\":\"不合规\",\"hits\":[{\"wordHitPositions\":[{\"positions\":[[0,2]],\"label\":\"500100\",\"keyword\":\"草泥马\"}],\"probability\":1.0,\"datasetName\":\"百度默认文本反作弊库\",\"words\":[\"草泥马\"],\"modelHitPositions\":[[0,2,0.9996]]}],\"subType\":5,\"conclusionType\":2,\"type\":12}]";
		Gson gson = new Gson();
		List<JsonBean> ps = gson.fromJson(strs, new TypeToken<List<JsonBean>>(){}.getType());
		List<Hits> hits = ps.get(0).getHits();
		for (Hits hit : hits) {
			 System.out.println(hit.getDatasetName());
		}
	}
}

代码相对比较简单,主要是需要注意字符串中集合数组类型问题。

~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

分享:

支付宝

微信