JSONObject本质是一个Map
JSONArray本质是一个List
String str = JSON.toJSONString(javaBean);
User user = JSON.parseObject(str,Entity.class);
String str = JSON.toJSONString(list)
List list = JSON.parseArray(str,Entity.class)
String str = JSON.toJSONString(array)
举例:
String[] initArray = {"Jack","Lili","Lucy"};
String str= JSON.toJSONString(initArray);
System.out.println(str);
console:
["Jack","Lili","Lucy"]
JSONObject json= (JSONObject) JSONObject.toJSON(javaBean);
User user= JSONObject.toJavaObject(json,User.class);
JSONObject json = JSON.parseObject(str);
String str= JSONObject.toJSONString(resultData);
JSONObject ObjOne = new JSONObject();
JObjOne.put("name", "Tom");
JSONObject ObjTwo = new JSONObject();
ObjTwo.put("sex", "男");
JSONObject ObjThree = new JSONObject();
ObjThree.put("age", 10);
JSONArray jsonArray = new JSONArray();
jsonArray.add(ObjOne);
jsonArray.add(ObjTwo);
jsonArray.add(ObjThree);
List list = jsonArray.toJavaList(User.class);
System.out.println(list);
Console:
[User(name=Tom, sex=null, age=null), User(name=null, sex=男, age=null), User(name=null, sex=null, age=10)]