现在的位置: 首页 > 综合 > 正文

java解析json字符串,多层解析

2019年07月19日 ⁄ 综合 ⁄ 共 2267字 ⁄ 字号 评论关闭

json泛型如下

{
    "code": 1,
    "message": "查询成功",
    "data": [
        {
            "type": 1,
            "question": "地层压力与同井深的淡水静液压力之比称为地层的()。",
            "answer": "1",
            "id": 1,
            "description": "题目描述",
            "answers": [
                {
                    "isCorrect": "1",
                    "answer_name": "A的选项内容"
                },
                {
                    "isCorrect": "0",
                    "answer_name": "B的选项内容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "C的选项内容"
                },
                {
                    "isCorect": "0",
                    "answer_name": "D的选项内容"
                }
            ]
        },
        {
            "type": 1,
            "question": "起钻时,产生的抽吸压力导致井底压力()。",
            "answer": "1",
            "id": 1,
            "description": "题目描述",
            "answers": [
                {
                    "isCorrect": 1,
                    "answer_name": "A的选项内容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "B的选项内容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "C的选项内容"
                },
                {
                    "isCorrect": 0,
                    "answer_name": "D的选项内容"
                }
            ]
        }
    ]
}

导入下面这几个jar包

如果没有可以到小编的资源中下载名叫json_jar  点击下载

commons-beanutils.jar

commons-collections.jar

commons-lang-2.6.jar

commons-logging-1.0.4.jar

ezmorph-1.0.6.jar

json-lib-2.4-jdk15.jar

java解析测试源码

import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class hhao {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		String jsonStr = "{\"code\":1,\"message\": \"查询成功\",\"data\": [{\"type\": 1,\"question\": \"题目名称题目名称题目名称题目1111111111111111111111111\",\"answer\": \"1\",\"id\":1,\"description\": \"题目描述\",\"answers\": [{\"isCorrect\": 1,\"answer_name\": \"A的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"B的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"C的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"D的选项内容\"}]},{\"type\": \"1\",\"question\": \"题目名称题目名称题目名称2222222222222222222222222222\",\"answer\": \"1\",\"id\": 1,\"description\": \"题目描述\",\"answers\": [{\"isCorrect\": 1,\"answer_name\": \"A的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"B的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"C的选项内容\"},{\"isCorrect\": 0,\"answer_name\": \"D的选项内容\"}]}]}";
		JSONObject job = JSONObject.fromObject(jsonStr);
		System.out.println("------1----->"+job.toString());
		JSONArray jArray = job.getJSONArray("data");
		Iterator<JSONArray> itr = jArray.iterator();
		while (itr.hasNext()) {
			JSONObject temp = JSONObject.fromObject(itr.next());
			temp.getInt("id");
			temp.getInt("type");
			temp.getString("question");
			System.out.println("------2----->"+temp.toString());
			JSONArray janswers = temp.getJSONArray("answers");
			Iterator<JSONArray> ianswers = janswers.iterator();
			while (ianswers.hasNext()) {
				JSONObject tanswers = JSONObject.fromObject(ianswers.next());
				tanswers.getInt("isCorrect");
				tanswers.getString("answer_name");
				System.out.println("------3----->"+tanswers.toString());
			}
			
		}
	}

}

抱歉!评论已关闭.