Parsing JSON array to java object

前端 未结 2 1125
南旧
南旧 2021-01-22 06:54

I am trying to parse using Jackson mapper to parse big JSON to java object. I have very big JSON but came across this little piece in it and not sure how to parse.<

相关标签:
2条回答
  • 2021-01-22 07:38

    Create one json pojo mapper class

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "coordinates"
    })
    public class Example {
    
    @JsonProperty("coordinates")
    private List<List<List<Double>>> coordinates = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    @JsonProperty("coordinates")
    public List<List<List<Double>>> getCoordinates() {
    return coordinates;
    }
    
    @JsonProperty("coordinates")
    public void setCoordinates(List<List<List<Double>>> coordinates) {
    this.coordinates = coordinates;
    }
    
    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    

    Then convert jsonString to pojo

    Gson gson = new GsonBuilder().create();
    Example r = gson.fromJson(jsonString, Example.class);
    
    0 讨论(0)
  • 2021-01-22 07:42

    It depends how really big is your JSON. If you can load it to memory, you can use the simplest way: Solution 1:
    POJO class:

    class CoordinatesContainer {
    
        private double[][][] coordinates;
    
        public double[][][] getCoordinates() {
            return coordinates;
        }
    
        public void setCoordinates(double[][][] coordinates) {
            this.coordinates = coordinates;
        }
    
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder(1024);
            for (double[] arrayItem : coordinates[0]) {
                builder.append(Arrays.toString(arrayItem));
                builder.append(", ");
            }
            return builder.toString();
        }
    }
    

    Usage:

    ObjectMapper mapper = new ObjectMapper();
    CoordinatesContainer coordinatesContainer = mapper.readValue(json, CoordinatesContainer.class);
    System.out.println(coordinatesContainer);
    

    Above program prints:

    [-72.943068, 45.842298], [-72.943075, 45.841859]
    

    Solution 2:
    But if your JSON is really big and you are not able to load it to memory, you should consider Jackson Streaming API. In this case you should not create POJO class and try to process each element "node" by "node":

    import java.io.File;
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.util.Arrays;
    
    import com.fasterxml.jackson.core.JsonFactory;
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.JsonToken;
    
    public class JsonProgram {
    
        public static void main(String[] args) throws IOException {
            File json = new File("/x/data.json");
            JsonFactory jsonFactory = new JsonFactory();
            JsonParser parser = jsonFactory.createParser(json);
            // Skip all elements to first array
            while (parser.nextToken() != JsonToken.START_ARRAY) {
            }
            parser.nextToken();
            // First level
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                // Skip inner start array element
                parser.nextToken();
                System.out.println();
                System.out.println("NEXT ARRAY NODE");
    
                BigDecimal first = parser.getDecimalValue();
    
                // Go to second value
                parser.nextToken();
    
                BigDecimal second = parser.getDecimalValue();
    
                // Skip inner end array element
                parser.nextToken();
    
                // Handle array item
                System.out.println("First: " + first.toString());
                System.out.println("Second: " + second.toString());
            }
        }
    }
    

    Above program prints:

    NEXT ARRAY NODE
    First: -72.943068
    Second: 45.842298
    
    NEXT ARRAY NODE
    First: -72.943075
    Second: 45.841859
    

    In my examples I used Jackson in 2.2.3 version.

    0 讨论(0)
提交回复
热议问题