一、Java中,JSONObject 为对象
如:
JSONObject json2 = new JSONObject();
json2.put("name", "王大");
json2.put("work", "厨师");
json2.put("love", "Tom");
json2.put("sy", "王大");
输出结果:{“love”:“Tom”,“work”:“厨师”,“sy”:“王大”,“name”:“王ff”}
通过
String json = new String();
json = json3.getString("sy");
获取"sy"这个key值获取值,输出结果为string: 王ff
二、JSONArray 为json数组:可将json对象加入到里面
JSONObject json2 = new JSONObject();
json2.put("name", "王大");
json2.put("work", "厨师");
json2.put("love", "Tom");
json2.put("sy", "王大");
System.out.println(json2);
JSONObject json3 = new JSONObject();
json3.put("name", "王s");
json3.put("work", "厨s");
json3.put("love", "Som");
json3.put("sy", "王ff");
JSONArray json4 = new JSONArray();
json4.add(json2);
json4.add(json3);
System.out.println(json4);
输出结果为:[{“love”:“Tom”,“work”:“厨师”,“sy”:“王大”,“name”:“王大”},{“love”:“Som”,“work”:“厨s”,“sy”:“王ff”,“name”:“王s”}]
三、获取多层嵌套数据方法:
JSONObject obj=
{
"formId": 255698,
"addTime": 1628858705,
"corpid": "ding99fc53f45455bd4635c2f4657eb6378f",
"data": {
"num_9": 0,
"text_14": "6fb8bc46-4410-c0c1-4a0f-0cf2c34ea967",
"text_13": "3",
"text_10": "2",
"text_2": "46852567",
"text_7": "32037157-1d1b-ba44-86f1-005275f19555",
"text_6": "5",
"text_9": "3",
"text_8": "3561114860-2067995216",
"num_10": 100,
"num_11": 2,
"num_12": 13540971018,
"num_13": 1,
"subForm_1": [
{
"subDataId": 892132,
"text_1": "13540971018"
}
],
"num_19": 1,
"num_32": 1,
"num_33": 0,
"num_34": 0,
"num_38": 1,
"num_30": 1209,
"num_31": 0,
"array_3": [
2882709
],
"array_5": [
"cb9a66cd-ddf9-ca2a-9ba8-b9438653d782"
],
"array_4": [
2187584,
5135951,
2187542,
11208832,
2187580,
2187550
],
"num_29": 41,
"num_5": 0,
"num_6": 0,
"num_7": 100,
"num_8": 0
},
"departmentId": 467071153,
"creatorId": "3561114860-2067995216",
"coUserId": "[]",
"del": 0,
"serialNo": "SO.20210812060",
"dataId": 8506832,
"appId": 31933,
"menuId": 242707,
"id": 8506832
}
JSONObject obj = (JSONObject) temp.get(i);
System.out.println("000000json1中text_8值" +obj.getJSONObject("data").getString("text_8"))
text_8的值为: 3561114860-2067995216
四、组合多层嵌套方法:
json4为jsonarray:[{“love”:“Tom”,“work”:“厨师”,“sy”:“王大”,“name”:“王大”},{“love”:“Som”,“work”:“厨s”,“sy”:“王ff”,“name”:“王s”}]
json1将jsonarray和jsonobject组合
JSONObject json2 = new JSONObject();
json2.put("name", "王大");
json2.put("work", "厨师");
json2.put("love", "Tom");
json2.put("sy", "王大");
System.out.println(json2);
JSONObject json3 = new JSONObject();
json3.put("name", "王s");
json3.put("work", "厨s");
json3.put("love", "Som");
json3.put("sy", "王ff");
JSONArray json4 = new JSONArray();
json4.add(json2);
json4.add(json3);
System.out.println(json4);
JSONObject json1 = new JSONObject();
json1.put("data", json4);
json1.put("love", "Som");
System.out.println(json1);