json数据的封装
程序员文章站
2022-03-27 08:26:38
...
工程引入org.json.jar包:
package com.ai.kfk.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
try {
JSONObject json1 = new JSONObject();
//--------------------------------------
JSONObject json2 = new JSONObject();
json2.put("json2Key1", "json2Value1");
json2.put("json2Key2", 15);
json2.put("json2Key3", true);
//--------------------------------------
JSONObject json3 = new JSONObject();
List<JSONObject> list = new ArrayList<JSONObject>();
JSONObject json4 = new JSONObject();
json4.put("json4Key1", "json4Value1");
json4.put("json4Key2", 15);
json4.put("json4Key3", true);
JSONObject json5 = new JSONObject();
json5.put("json5Key1", "json5Value1");
json5.put("json5Key2", 15);
json5.put("json5Key3", true);
JSONObject json6 = new JSONObject();
json6.put("json6Key1", "json6Value1");
json6.put("json6Key2", 15);
json6.put("json6Key3", true);
list.add(json4);
list.add(json5);
list.add(json6);
json3.put("json3Key1", list);
//--------------------------------------
json1.put("json1Key1", "json1Value1");
json1.put("json1Key2", 15);
json1.put("json1Key3", true);
json1.put("json1Key4", json2);
json1.put("json1Key5", json3);
System.out.println(json1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
控制台打印:
{"json1Key1":"json1Value1","json1Key5":{"json3Key1":[{"json4Key3":true,"json4Key2":15,"json4Key1":"json4Value1"},{"json5Key2":15,"json5Key3":true,"json5Key1":"json5Value1"},{"json6Key2":15,"json6Key1":"json6Value1","json6Key3":true}]},"json1Key4":{"json2Key1":"json2Value1","json2Key2":15,"json2Key3":true},"json1Key3":true,"json1Key2":15}
显示json数据:
上一篇: 去掉点击链接后的虚线框
下一篇: Node 模块