一、序列化和反序列化
序列化:将java对象转为可存储的形式【java转json字符串】
反序列化:将文件转成java对象【json字符串转java】
二、fastjson
总结:
- java和json对象或字符串的直接转化就操作JSON的方法
- 只有Json对象转成json字符串的时候才调用json对象的方法
- 使用JSONObject自定义json对象
1.Java对象–>Json对象
| |
| |
| Person person = new Person(); |
| person.setName("张三"); |
| person.setAge(25); |
| JSONObject jsonObject = (JSONObject) JSON.toJSON(person); |
复制
2.Java对象–>Json字符串
| |
| |
| Person person = new Person(); |
| person.setName("张三"); |
| person.setAge(25); |
| String jsonString = JSON.toJSONString(person); |
复制
3.Json对象-> Json字符串
| |
| |
| JSONObject jsonObject = (JSONObject) JSON.toJSON(person); |
| String jsonString = jsonObject.toJSONString(); |
复制
4.JSON字符串–>JSON对象
| |
| |
| String jsonString = "{\"name\":\"张三\",\"age\":25}"; |
| JSONObject jsonObject = JSON.parseObject(jsonString); |
复制
5.Json对象->Java对象
| |
| |
| String jsonString = "{\"name\":\"张三\",\"age\":25}"; |
| JSONObject jsonObject = JSON.parseObject(jsonString); |
| Person person = JSON.toJavaObject(jsonObject, Person.class); |
复制
6.Json字符串->Java对象
| |
| |
| String jsonString = "{\"name\":\"张三\",\"age\":25}"; |
| Person person = JSON.parseObject(jsonString, Person.class); |
复制
7.自定义Json
| |
| JSONObject jsonObject = new JSONObject(); |
| |
| |
| jsonObject.put("name", "张三"); |
| jsonObject.put("age", 25); |
| jsonObject.put("isStudent", false); |
| |
| |
| JSONObject addressObject = new JSONObject(); |
| addressObject.put("city", "北京"); |
| addressObject.put("street", "长安街"); |
| addressObject.put("postalCode", "100000"); |
| |
| |
| jsonObject.put("address", addressObject); |
| |
| System.out.println(jsonObject.toJSONString()); |
| |
| |
复制
三、Jackson
说明:
- Jackson中JsonNode是json对象
- 一般调用ObjectMapper的方法即可进行转换
1.Java对象–>Json字符串
| |
| |
| Person person = new Person(); |
| person.setName("张三"); |
| person.setAge(30); |
| ObjectMapper objectMapper = new ObjectMapper(); |
| String jsonStr = objectMapper.writeValueAsString(person); |
复制
2.Java对象->Json对象
| |
| |
| Person person = new Person(); |
| person.setName("张三"); |
| person.setAge(30); |
| ObjectMapper objectMapper = new ObjectMapper(); |
| JsonNode jsonNode = objectMapper.valueToTree(person); |
复制
3.Json对象-> Json字符串
| |
| |
| ObjectMapper objectMapper = new ObjectMapper(); |
| JsonNode jsonNode = objectMapper.readTree("{\"name\":\"张三\",\"age\":30}"); |
| String jsonStr = objectMapper.writeValueAsString(jsonNode); |
复制
4.JSON字符串–>JSON对象
| |
| |
| String jsonStr = "{\"name\":\"张三\",\"age\":30}"; |
| ObjectMapper objectMapper = new ObjectMapper(); |
| JsonNode jsonNode = objectMapper.readTree(jsonStr); |
复制
5.Json对象->Java对象
| |
| |
| String jsonStr = "{\"name\":\"张三\",\"age\":30}"; |
| ObjectMapper objectMapper = new ObjectMapper(); |
| JsonNode jsonNode = objectMapper.readTree(jsonStr); |
| Person person = objectMapper.treeToValue(jsonNode, Person.class); |
复制
6.Json字符串->Java对象
| |
| |
| String jsonStr = "{\"name\":\"张三\",\"age\":30}"; |
| ObjectMapper objectMapper = new ObjectMapper(); |
| Person person = objectMapper.readValue(jsonStr, Person.class); |
复制
7.自定义Json
| |
| |
| |
| ObjectMapper objectMapper = new ObjectMapper(); |
| |
| ObjectNode outerJsonNode = objectMapper.createObjectNode(); |
| |
| outerJsonNode.put("name", "张三"); |
| outerJsonNode.put("age", 30); |
| outerJsonNode.put("isMarried", false); |
| |
| |
| ObjectNode innerJsonNode = objectMapper.createObjectNode(); |
| |
| |
| innerJsonNode.put("city", "北京"); |
| innerJsonNode.put("country", "中国"); |
| |
| |
| outerJsonNode.set("address", innerJsonNode); |
| |
| |
| System.out.println("自定义JsonNode: " + outerJsonNode.toString()); |
复制