Java如何获取JSON数据中的值 备忘

Java如何获取JSON数据中的值

取出JsonArray中的object(orderNo)

嵌套
JsonObeject {
   JsonObeject {
      JsonArray [Obejet]
   }
}

{
  "rlt": true,
  "message": "操作成功!",
  "code": "000000",
  "data": {
    "records": [
      {
        "orderNo": "ord_010_20220111111111111",
        "deleted": 0,
        "type": 1
      },
      {
        "orderNo": "ord_010_20220222222222222",
        "deleted": 1,
        "type": 2
      }
    ],
    "totalCount": 1,
    "pageSize": 10,
    "pageNo": 1,
  }
}
        //类型转化 string转jsonObject 拿到这个json
        JSONObject object = (JSONObject) JSONObject.parse(body);
  

        JSONArray records=object.getJSONObject("data").getJSONArray("records");
        System.out.println(records);
        /*
        [
		      {
		        "orderNo": "ord_010_20220111111111111",
		        "deleted": 0,
		        "type": 1
		      },
		      {
		        "orderNo": "ord_010_20220222222222222",
		        "deleted": 1,
		        "type": 2
		      }
      	] 
      	*/


        JSONObject object0=records.getJSONObject(0);
        System.out.println(object0);
		/*
		{
			 "orderNo": "ord_010_20220111111111111",
			 "deleted": 0,
			 "type": 1
		}
		*/



        String orderNo=(String)object0.get("orderNo") ;
        System.out.println(orderNo);
        //ord_010_20220111111111111

取出JsonObject中的object

//拿到出参数给下个接口入参
JSONObject object = (JSONObject) JSONObject.parse(body);
System.out.println(object.getJSONObject("data").get("orderNo"));



//拿到返回判断正确不
JSONObject object = (JSONObject) JSONObject.parse(body);
//String orderToken=(String) object.getJSONObject("data").get("orderToken");    //强转类型string
String rlt= object.getString("rlt");
String message= object.getString("message");
System.out.println("\n结果判断:\n"+"rlt:"+rlt+"    message:"+message);

{
  "code": "000000",
  "data": {
    "orderId": "111111",
    "orderNo": "ord_010_111111",
    "studentUid": "111111",
  "rlt": true,
  "message": "ok"
}

相关博客1

相关博客2

相关博客3

你可能感兴趣的:(JAVA,笔记,问题解决,java,json,开发语言)