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 成为理想的数据交换语言—— 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

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"
    }
}

代码如下所示。

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

猜你喜欢

转载自blog.csdn.net/u010804417/article/details/130057852