Parsing JSON array to java object

前端 未结 2 1124
南旧
南旧 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>> coordinates = null;
    @JsonIgnore
    private Map additionalProperties = new HashMap();
    
    @JsonProperty("coordinates")
    public List>> getCoordinates() {
    return coordinates;
    }
    
    @JsonProperty("coordinates")
    public void setCoordinates(List>> coordinates) {
    this.coordinates = coordinates;
    }
    
    @JsonAnyGetter
    public Map 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);
    

提交回复
热议问题