,JSONArray互相转化

时间:2025-02-16 08:17:48

JSONObject本质是一个Map
JSONArray本质是一个List

对象转JSON

String str = (javaBean);

转java对象

User user = (str,);

转JSON

String str = (list)

转List

List list = (str,)

5.数组转JSON

String str = (array)

举例:

String[] initArray = {"Jack","Lili","Lucy"};
String str= (initArray);
(str);

console:
["Jack","Lili","Lucy"]

6. java对象转JSONObject

JSONObject json= (JSONObject) (javaBean); 

7. JSONObject转java对象

User user= (json,);

8. 字符串转JSONObject

JSONObject json = (str);

转字符串

String str= (resultData);

转list

JSONObject ObjOne = new JSONObject();
("name", "Tom");
JSONObject ObjTwo = new JSONObject();
("sex", "男");
JSONObject ObjThree = new JSONObject();
("age", 10);

JSONArray jsonArray = new JSONArray();
 (ObjOne);
 (ObjTwo);
(ObjThree);
List<User> list = ();
(list);

Console:
[User(name=Tom, sex=null, age=null), User(name=null, sex=男, age=null), User(name=null, sex=null, age=10)]