|
| 1 | +package biz.chenxu.tutorial; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class JacksonApplication { |
| 12 | + |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 15 | + |
| 16 | + String json = "{\n" + |
| 17 | + " \"stringField\": \"demoData\",\n" + |
| 18 | + " \"dateField\": \"2020-02-23\",\n" + |
| 19 | + " \"intField\": 1,\n" + |
| 20 | + " \"doubleField\": 1.0,\n" + |
| 21 | + " \"floatField\": 1.0,\n" + |
| 22 | + " \"complexTypeList\": [\n" + |
| 23 | + " {\n" + |
| 24 | + " \"stringField\": \"innerDemoData\",\n" + |
| 25 | + " \"dateField\": \"2020-02-23\",\n" + |
| 26 | + " \"intField\": 1,\n" + |
| 27 | + " \"doubleField\": 1.0,\n" + |
| 28 | + " \"floatField\": 1.0\n" + |
| 29 | + " }\n" + |
| 30 | + " ]\n" + |
| 31 | + "}"; |
| 32 | + |
| 33 | + JsonNode rootNode = objectMapper.readTree(json); |
| 34 | + JsonNode listNode = rootNode.get("complexTypeList"); |
| 35 | + JsonParser token = objectMapper.treeAsTokens(listNode); |
| 36 | + List<ComplexType> list = objectMapper.readValue(token, new TypeReference<List<ComplexType>>(){}); |
| 37 | + for (ComplexType complexType : list) { |
| 38 | + System.out.println(complexType); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments