Bebop
Bebop

Reputation: 61

How to parse json value without keys using RestTemplate?

I want to parse json value but there are some value without keys.

like this

"geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        127.08373748622218,
                        37.529508099979225
                    ],
                    [
                        127.08355138558433,
                        37.529735847943925
                    ]
                ]
            }

Geometry.class

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Geometry {
    private String type;
    private Coordinate coordinates;
}

Coordinate.class

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Coordinate {
    private List<Double[]> xy;
}

then i saw this error

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `kr.seoulmaas.ieye.service.dto.path.walk.Coordinate` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 133] (through reference chain: kr.seoulmaas.ieye.service.dto.path.WalkPathResDto["features"]->java.util.ArrayList[0]->kr.seoulmaas.ieye.service.dto.path.walk.Feature["geometry"]->kr.seoulmaas.ieye.service.dto.path.walk.Geometry["coordinates"])

Upvotes: 0

Views: 722

Answers (3)

Bebop
Bebop

Reputation: 61

Sorry, I'm dreadfully late.

I solved this problem using by JsonNode.

this is my code.

public class Geometry {
    private String type;
    private JsonNode coordinates;
}

Upvotes: 0

user404
user404

Reputation: 2028

In your data, coordinates is a list of list. So define your Geometry class this way:

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Geometry {
    private String type;
    private List<Coordinate> coordinates;
}

Hope this helps.

Upvotes: 0

devgianlu
devgianlu

Reputation: 1580

private Coordinate coordinates; should be private List<Coordinate> coordinates; since it is an array in the JSON, but since the array contains only arrays you need to have a list of lists: List<List<Double>> coordinates (suggested by @JBNizet).

Upvotes: 1

Related Questions