[Jackson] ObjectMapper is used to deserialize the JSON containing any key. objectmapperjson
Use ObjectMapper to deserialize JSON containing any key
After a RESTful API is called, the returned JSON string contains no predefined key. Compared with the fixed JSON structure, it requires some additional operations.
For JSON with fixed structures, using ObjectMapper in combination with a predefined object type can be very convenient for deserialization, such as for the following JSON:
GET /person/1{ "id": "1", "name": "dm_vincent", "age": "28"}
Combined with an object type, you can easily complete deserialization:
public class Person { public long id; public String name; public int age;}
public static <T> T getEntity(String jsonString, Class<T> prototype) { ObjectMapper objectMapper = new ObjectMapper(); try { return (T) objectMapper.readValue(jsonString, prototype); } catch (IOException e) { e.printStackTrace(); } return null; }
However, some APIs that support multiple records at a time may encounter problems. For example, you have an API in the following format:
GET /person/1,2,3{ "dm_vincent": { "id": "1", "name": "dm_vincent", "age": "28" }, "dm_vincent2": { "id": "2", "name": "dm_vincent2", "age": "29" }, "dm_vincent3": { "id": "3", "name": "dm_vincent3", "age": "30" }}
Although the object type to be obtained is still the Person class, it is not as simple and clear as above. For example, the following code may cause problems during deserialization:
public class PersonWrapper { public Map<String, Person> persons;}
Our intention is clear: put multiple Person object returned in a Map structure. However, the problem is that the returned keys in JSON are not fixed (for example, the keys in the preceding JSON are people's names), which leads to deserialization failure. After all, the ObjectMapper under the default configuration is not so smart as to this extent, you can guess that you want to put multiple entities into Map.
One of the correct methods is to use the readTree method of ObjectMapper:
public static <T> EntityWrapper<T> getEntityWrapper(String jsonString, Class<T> prototype) { ObjectMapper objectMapper = new ObjectMapper(); EntityWrapper<T> wrapper = new EntityWrapper<T>(); try { JsonNode root = objectMapper.readTree(jsonString); Iterator<Entry<String, JsonNode>> elements = root.fields(); while (elements.hasNext()) { Entry<String, JsonNode> node = elements.next(); String key = node.getKey(); T element = objectMapper.readValue(node.getValue().toString(), prototype); wrapper.addEntry(key, element); } return wrapper; } catch (IOException e) { e.printStackTrace(); } return null; }
The above code is briefly explained:
Useroot.field()
Method To obtain all key-value pairs in the returned JSON.
Then cyclically extract the key and value of a key-value pair. For value, we can directly use the previous policy for deserialization, because the structure of this part is also fixed.
Ignore unnecessary Fields
Sometimes, if the returned JSON string contains a field that we do not need, an exception is thrown when the corresponding object class does not contain this field, it tells you that some fields are not found in the object class. The solution is simple. After declaring ObjectMapper, add the above Code:
ObjectMapper objectMapper = new ObjectMapper();objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.