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

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());
			}
			
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值