JSONObject的使用,与Java对象、String、List之间的转换

工作中用到json、String、Java对象间的各种转换,在此梳理一下,以备不时之需。

JSONObject的使用

1、pom.xml文件添加依赖

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.83</version>
</dependency>

2、 说明: JSONObject继承了JSON类,实现了Map接口,内部结构为key-value格式。

public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler
  • 1

3、一些常用的方法示例

JSONObject jsonObject1 = new JSONObject();
System.out.println(jsonObject1.size());
System.out.println(jsonObject1.isEmpty());
jsonObject1.put("1","zhang1");
jsonObject1.put("2","zhang2");
jsonObject1.put("3","zhang3");
jsonObject1.put("4","zhang4");
//长度
System.out.println(jsonObject1.size());
//是否为空
System.out.println(jsonObject1.isEmpty());
//JSON对象->String
System.out.println(jsonObject1.toJSONString());
System.out.println(jsonObject1.toString());
//是否包含key
System.out.println(jsonObject1.containsKey("7"));
//是否包含value
System.out.println(jsonObject1.containsValue("zhang4"));
//遍历key
Set<String> keys = jsonObject1.keySet();
for(String k: keys){
  System.out.println(k);
}
//遍历value
Set<Map.Entry<String, Object>> values = jsonObject1.entrySet();
for(Map.Entry<String, Object> v: values){
  System.out.println(v);
}

JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name","张三");
jsonObject2.put("dept","研发");
jsonObject2.put("age","22");
jsonObject2.put("gender","女");

JSONObject jsonObject = new JSONObject();
jsonObject.put("jsonObject1",jsonObject1);
jsonObject.put("jsonObject2",jsonObject2);
System.out.println(jsonObject);

//JSON对象->Java对象(获取某个节点)1
UserInfo user1 = jsonObject.getObject("jsonObject2", UserInfo.class);
System.out.println(user1.toString());

//JSON对象->Java对象(获取某个节点)2
UserInfo user2 = jsonObject.getJSONObject("jsonObject2").toJavaObject(UserInfo.class);
System.out.println(user2.toString());

//数组
JSONArray jsonArray = new JSONArray();
jsonArray.add("111111");
jsonArray.add("222222");
jsonArray.add("333333");
System.out.println(jsonArray.toString());

//将数组放入JSON对象中
jsonObject.put("jsonArray",jsonArray);
System.out.println(jsonObject);

//获取数组对象
JSONArray aa = jsonObject.getJSONArray("jsonArray");

//获取JSON对象
JSONObject bb = jsonObject.getJSONObject("jsonObject1");
JSONObject cc = jsonObject.getJSONObject("jsonObject2");
System.out.println(aa);
System.out.println(bb);
System.out.println(cc);
System.out.println(jsonObject.toJSONString());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

Java对象的转换

1、定义Java对象

UserInfo userInfo = new UserInfo();
userInfo.setName("zhang");
userInfo.setDept("yanfa");
userInfo.setGender("女");
userInfo.setAge(11);
  • 1
  • 2
  • 3
  • 4
  • 5

2、Java对象->String

String userStr = JSON.toJSONString(userInfo);
  • 1

3、Java对象->JSON对象

JSONObject userStrJson2 = (JSONObject) JSON.toJSON(userInfo);
  • 1

JSON对象的转换

1、定义JSON对象

UserInfo userInfo = new UserInfo();
userInfo.setName("zhang");
userInfo.setDept("yanfa");
userInfo.setGender("女");
userInfo.setAge(11);
JSONObject json = (JSONObject) JSON.toJSON(userInfo);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、JSON对象->Java对象

UserInfo user = JSONObject.toJavaObject(json, UserInfo.class);
  • 1

3、JSON对象->String

String str = JSONObject.toJSONString(json);
  • 1

String的转换

1、定义String串

String str = "{\"gender\":\"女\",\"name\":\"张三\",\"dept\":\"研发\",\"age\":\"22\"}";
  • 1

2、String->Java对象

UserInfo user = JSONObject.parseObject(str, UserInfo.class);
  • 1

3、String->JSON对象

JSONObject json = JSONObject.parseObject(str);
  • 1

List的转换

1、定义List对象

List<UserInfo> userList = new ArrayList<UserInfo>();
UserInfo user1 = new UserInfo();
user1.setName("zhang");
user1.setDept("yanfa");
user1.setGender("女");
user1.setAge(11);
UserInfo user2 = new UserInfo();
user2.setName("zhang");
user2.setDept("yanfa");
user2.setGender("女");
user2.setAge(11);
userList.add(user1);
userList.add(user2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2、List->String

String str1 = JSON.toJSONString(userList);
  • 1

3、List->JSONObject->String

String str2 = JSON.toJSON(userList).toString();
  • 1

4、String->JSONArray

JSONArray array = JSONObject.parseArray(str1);
  • 1
 
posted @ 2022-08-04 16:07  甜菜波波  阅读(873)  评论(0编辑  收藏  举报