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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
class User {
private Long id;
private String userId;
private String userName;
private Address[] address;
@Data
public static class Address{
private String street;
}
}
public class A {
public static void main(String[] args) throws IOException {
// jackson
ObjectMapper objectMapper = new ObjectMapper();
User user = new User();
user.setId(1L);
user.setUserId("123");
user.setUserName("test");
User.Address address = new User.Address();
address.setStreet("BJ");
User.Address[] list = new User.Address[]{address};
user.setAddress(list);
System.out.println("----jackson----");
//转为json
String json = objectMapper.writeValueAsString(user);
System.out.println("Bean > json " + json);
String jsonString = "{\"id\":1,\"userId\":\"123\",\"userName\":\"test\",\"address\":[{\"street\":\"BJ\"}]}";
//转为Bean
User userBean = objectMapper.readValue(jsonString, User.class);
System.out.println("json > Bean " + userBean);
//不转为Bean
JsonNode rootNode = objectMapper.readTree(jsonString);
// asInt asText
String userName = rootNode.path("userName").asText();
System.out.println("json > 非Bean属性 " + userName);
int userId = rootNode.path("userId").asInt();
System.out.println("json > 非Bean属性 " + userId);
JsonNode childNode = rootNode.path("address");
// get(0)用于获取列表元素
String street = childNode.get(0).path("street").asText();
System.out.println("json > 非Bean子属性 " + street);
//转为Bean Map
Map<String, Object> map = objectMapper.readValue(jsonString, new TypeReference<Map<String, Object>>(){});
// 或者 HashMap map = objectMapper.readValue(string, HashMap.class);
System.out.println("json > Map " + map);
String jsonStringArray = "[{\"id\":1,\"userId\":\"123\",\"userName\":\"test\",\"address\":[{\"street\":\"street1\"}]}," +
"{\"id\":1,\"userId\":\"123\",\"userName\":\"test\",\"address\":[{\"street\":\"street1\"}]}]";
//转为Bean List
List<User> userList = objectMapper.readValue(jsonStringArray, new TypeReference<List<User>>(){});
System.out.println("json > List<Bean> " + userList);
// fastjson
System.out.println("----fastjson----");
//转为json
String string = JSON.toJSONString(user);
System.out.println("Bean > json " + string);
// 转为Bean
user = JSON.parseObject(jsonString, User.class);
System.out.println("json > Bean " + user);
// 转为非Bean
JSONObject bodyJson = JSON.parseObject(jsonString);
System.out.println(bodyJson);
userName = bodyJson.getString("userName");
System.out.println("json > 非Bean属性 " + userName);
userId = bodyJson.getIntValue("userId");
System.out.println("json > 非Bean属性 " + userId);
street = bodyJson.getJSONArray("address").getJSONObject(0).getString("street");
System.out.println("json > 非Bean子属性 " + street);
// 转为Bean List
userList = JSON.parseArray(jsonStringArray, User.class);
System.out.println("json > List<Bean> " + userList);
}
}
|