(1)是什么?
阿里巴巴提供的一个json快速转换工具类,实现了Map接口,通过Key Value形式存储数据,初始化大小为16,有参构造方法需要传入一个Map。
项目中实际用途:
用于Json转具体的Object实体类,或者String快速转JSON格式,都可以用
或者有的数据属性变化较多,暂时不想定义一个POJO接参,就可以用JSONObject先接着。
(2)怎么用?
基本用法:public static void main(String[] args) throws Exception {// (1)通过无参构造直接设值JSONObject object = new JSONObject();object.put("id",123);object.put("name","jojo");System.out.println("JSONObject:"+object.toString());// (2)将JSON对象转成具体的POJODemo demo = JSON.toJavaObject(object, Demo.class);System.out.println("转换成Demo实体类:"+demo.toString());// (3)传入Map的有参构造方法创建Map<String,Object> myMap = new HashMap<>(2);myMap.put("id",123);myMap.put("name","test");JSONObject newObject = new JSONObject(myMap);System.out.println("通过有参构造传入Map创建出来的:"+newObject);// (4)将String转JSONObjectString demoString = "{name:\"test\", id:123}";JSONObject demoJson = JSONObject.parseObject(demoString);System.out.println("String转Object : "+demoJson);
}
结果:
JSONObject:{"name":"jojo","id":123}
转换成Demo实体类:Demo{id='123', name='jojo'}
通过有参构造传入Map创建出来的:{"name":"test","id":123}
String转Object : {"name":"test","id":123}Process finished with exit code 0
进阶玩法:String格式的List转List
public static void main(String[] args) throws Exception {String listStr = "[{\"id\":1,\"name\":\"vivi\"},{\"id\":2,\"name\":\"jojo\"}]";// (1)转Json ListJSONArray jsonArray = JSONArray.parseArray(listStr);System.out.println(jsonArray);// (2)转具体的List<实体类>List<Demo> listDemo = jsonArray.toJavaList(Demo.class);for (Demo demo : listDemo){System.out.println(demo.toString());}
}
遍历JSONArray
遍历输出:
public static void main(String[] args) throws Exception {String listStr = "[{\"id\":1,\"name\":\"vivi\"},{\"id\":2,\"name\":\"jojo\"}]";// (1)转Json ListJSONArray jsonArray = JSONArray.parseArray(listStr);System.out.println(jsonArray);// (2) 循环遍历JsonArrayfor (int i = 0; i < jsonArray.size(); i++) {System.out.println(jsonArray.getJSONObject(i));System.out.println(jsonArray.getJSONObject(i).get("name"));}
}
[{"name":"vivi","id":1},{"name":"jojo","id":2}]
{"name":"vivi","id":1}
vivi
{"name":"jojo","id":2}
jojo
遍历JSONObject
public static void main(String[] args) throws Exception {JSONObject jsonObject = new JSONObject();jsonObject.put("1","a");jsonObject.put("2","b");jsonObject.put("3","c");for (String a : jsonObject.keySet()){System.out.println("key:"+a+";Value:"+jsonObject.get(a));}
}
key:1;Value:a
key:2;Value:b
key:3;Value:c
迭代器方式遍历JSONObject
public static void main(String[] args) throws Exception {JSONObject jsonObject = new JSONObject();jsonObject.put("1","a");jsonObject.put("2","b");jsonObject.put("3","c");Iterator iterator = jsonObject.entrySet().iterator();while (iterator.hasNext()){Map.Entry entry = (Map.Entry) iterator.next();System.out.println("key: "+entry.getKey().toString()+";value: "+entry.getValue().toString());}
}
key: 1;value: a
key: 2;value: b
key: 3;value: c
也可以写成entrySet遍历方式:
public static void main(String[] args) throws Exception {JSONObject jsonObject = new JSONObject();jsonObject.put("1","a");jsonObject.put("2","b");jsonObject.put("3","c");for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {System.out.println("key: " + ((Map.Entry) stringObjectEntry).getKey().toString()+ ";value: " + stringObjectEntry.getValue().toString());}
}