Java 读/写 json串中的数据内容

JSON简介

JSON,全名JavaScript Object Notation, 即“JS对象简谱”。JSON是一种轻量级的数据交换格式,其基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,也是基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言—— 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

各JSON的Maven坐标

查询此博客

org.json.simple.JSONObject

读写格式以下方json串为例。

{
	"errcode":0,
	"errmsg":"ok",
	"access_token":"ena_OvqtddoX7h-hVL9GlmgOYmKpLh1EakBMX2a_BCeJXg-M8bIMDtAwEL41-hVL_O1Kgjgu8MkEXVxGHrojWu83X7CQSx3YACJphVjF5UvAOl5jLBq-2pspOiBbA1Oi-r7p2lHZmYWg_063poofk5FXFsO41bogSROA2dT3fFXxb82KXKkmjkZG8kOphrmqWlrvnvzEVLo9-rMsvGn_Mw",
	"expires_in":7200
}

代码如下所示。

			import org.json.simple.parser.JSONParser;
			import org.json.simple.JSONObject;

			String jsonString ={"errcode":0,"errmsg":"ok","access_token":"ena_OvqtddoX7h-hVL9GlmgOYmKpLh1EakBMX2a_BCeJXg-M8bIMDtAwEL41-hVL_O1Kgjgu8MkEXVxGHrojWu83X7CQSx3YACJphVjF5UvAOl5jLBq-2pspOiBbA1Oi-r7p2lHZmYWg_063poofk5FXFsO41bogSROA2dT3fFXxb82KXKkmjkZG8kOphrmqWlrvnvzEVLo9-rMsvGn_Mw","expires_in":7200}”
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString );
			String errcode = jsonObject.get("errcode").toString();
			String errmsg = jsonObject.get("errmsg").toString();
			if (errcode.equals("0") && errmsg.equals("ok")) {
				String resultFromGET_UrlString = jsonObject.get("access_token").toString();
				System.out.println("请求企业微信的token结果是"+resultFromGET_UrlString);
			}else {
				System.out.println("请求返回信息报错");
			}

com.alibaba.fastjson.JSONObject

读写格式以下方json串为例。

{
	"code":0,
	"message":"ok",
	"body":{
        "result": "success",
        "status": "1"
    }
}

com.alibaba.fastjson.JSONObjectjsonObject .put("code", 0);会覆盖相同键名的键值对的值,即如果前面一句是jsonObject .put("code", 0);、后面一句是jsonObject .put("code", 1);、最终执行的结果是jsonObject的code为1、而非jsonObject出现两个code一个是1一个是2。

import com.alibaba.fastjson.JSONObject;

		JSONObject resultJsonObject = new JSONObject();
        resultJsonObject.put("code", 0);
        resultJsonObject.put("message", "ok");

        JSONObject subJsonObject = new JSONObject();
        subJsonObject.put("result", "success");
        subJsonObject.put("status", "1");

        resultJsonObject.put("body", subJsonObject);
        System.out.println(resultJsonObject);

上方代码在控制台输出的结果,如下图所示。
在这里插入图片描述

代码如下所示。

import com.alibaba.fastjson.JSONObject;


		String jsonString = "{\"code\":0,\"message\":\"ok\",\"body\":{\"result\": \"success\",\"status\": \"1\"}}";
		
		JSONObject jsonObject = JSONObject.parseObject(jsonString);

        String result = jsonObject.getJSONObject("body").getString("result");
        String status = jsonObject.getJSONObject("body").getString("status");
        //输出结果为“获取到Json中的result是success,status是1”
        System.out.println("获取到Json中的result是"+result+",status是"+status); 
        
        //也提供了读取jsonArray的方法
        JSONArray eventList_jsonArray = testJsonObject.getJSONArray("eventList");

		//读取eventList_jsonArray中的第一个元素中“courseCode”键值对
		String courseCode = eventList_jsonArray.getJSONObject(0).getString("courseCode")
		
		//读取JSONObject内层jsonArray中的jsonArray,自测
				String testJsonString = "{\"studentName\":\"1\",\"courseList\":[{\"courseOrder\":\"1\",\"courseCode\":\"7E\",\"courseLenght\":\"39\",\"courseType\":\"3\",\"courseContentList\":[{\"courseContentOrder\":1,\"courseContentValue\":\"01\"},{\"courseContentOrder\":2,\"courseContentValue\":\"02\"},{\"courseContentOrder\":3,\"courseContentValue\":\"03\"},{\"courseContentOrder\":4,\"courseContentValue\":\"04\"},{\"courseContentOrder\":5,\"courseContentValue\":\"05\"},{\"courseContentOrder\":6,\"courseContentValue\":\"06\"},{\"courseContentOrder\":7,\"courseContentValue\":\"07\"}]}]}";
	JSONObject testJsonObject = JSONObject.parseObject(testJsonString);
		JSONArray courseContentList_jsonArray = testJsonObject.getJSONArray("courseContentList");
		for (Iterator<Object> iterator = courseContentList_jsonArray.iterator(); iterator.hasNext(); ) {
			JSONObject tempJsonObject = (JSONObject) iterator.next();
			JSONArray courseContentListJsonArray = tempJsonObject.getJSONArray("courseContentList");
			System.out.println("courseContentList===>>> " + courseContentListJsonArray );
			//int value = courseContentListJsonArray .getIntValue(i);//还可以通过此方法取出JsonArray中的第i个值
		}  
        
        

net.sf.json.JSONObject

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
		
		//写
        JSONArray dataarray = new JSONArray();
        JSONArray childrenarray1 = new JSONArray();
        JSONArray childrenarray2 = new JSONArray();
        JSONArray childrenarray3 = new JSONArray();
        
        JSONObject result = new JSONObject();
        JSONObject datajson = new JSONObject();
        JSONObject childrenjson1 = new JSONObject();
        JSONObject childrenjson2 = new JSONObject();
        JSONObject childrenjson3 = new JSONObject();
        
        childrenjson3.put("LCode", "LCode");
        childrenjson3.put("QName", "QName");
        childrenjson3.put("OId", "66666666666666");
        childrenjson3.put("Time", "expEndTime");
        childrenjson3.put("Time", "111");
        childrenjson3.put("type", "222");
        childrenjson3.put("EBTime", "666");
        childrenjson3.put("EId", "888");
        childrenjson3.put("status", "已提交");

        childrenarray3.add(childrenjson3);
        childrenjson2.put("children", childrenarray3);
        childrenarray2.add(childrenjson2);
        childrenjson1.put("children", childrenarray2);
        childrenarray1.add(childrenjson1);
        datajson.put("children", childrenarray1);

        dataarray.add(datajson);
        result.put("data", dataarray);

读上述写的json串的代码如下所示。

//读
String testJsonString = "{"studentId":"1684","studentPropertyList":[{"studentOrder":"1","paramList":[{"paraOrder":1,"paraValue":"1"},{"paraOrder":2,"paraValue":"2"},{"paraOrder":3,"paraValue":"3"},{"paraOrder":4,"paraValue":"4"},{"paraOrder":5,"paraValue":"5"},{"paraOrder":6,"paraValue":"6"},{"paraOrder":7,"paraValue":"7"}]}]}";
JSONObject jsonObject = JSONObject.fromObject(testJsonString);//创建json对象
JSONArray jsonArray = jsonObject.getJSONArray("eventList");
for(int i=0;i<jsonArray.size();i++) {
	JSONObject jsonObjectFromJsonArray = jsonArray.getJSONObject(i);
	JSONArray paramListJsonArray = jsonObjectFromJsonArray.getJSONArray("paramList");//取出paramList			
}

//读,另一例子
String QNameString = (String) result.getJSONArray("data").getJSONObject(0).getJSONArray("children").getJSONObject(0).getJSONArray("children").getJSONObject(0).getJSONArray("children").getJSONObject(0).getString("QName");
        System.out.println("从json中取出的QName是:"+QNameString );
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值