如何使用 RestTemplate 解析没有键的 json 值?

我想解析 json 值,但有些值没有键。


像这样


"geometry": {

                "type": "LineString",

                "coordinates": [

                    [

                        127.08373748622218,

                        37.529508099979225

                    ],

                    [

                        127.08355138558433,

                        37.529735847943925

                    ]

                ]

            }

几何类


@Getter

@NoArgsConstructor(access = AccessLevel.PROTECTED)

@ToString

public class Geometry {

    private String type;

    private Coordinate coordinates;

}


坐标类


@Getter

@NoArgsConstructor(access = AccessLevel.PROTECTED)

@ToString

public class Coordinate {

    private List<Double[]> xy;

}

然后我看到了这个错误


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"])


慕森卡
浏览 137回答 3
3回答

MMTTMM

private Coordinate coordinates;应该是private List<Coordinate> coordinates;因为它是 JSON 中的一个数组,但由于该数组仅包含数组,因此您需要有一个列表列表:List<List<Double>> coordinates(@JBNizet 建议)。

千巷猫影

对不起,我来晚了。我使用 JsonNode 解决了这个问题。这是我的代码。public class Geometry {&nbsp; &nbsp; private String type;&nbsp; &nbsp; private JsonNode coordinates;}

桃花长相依

在您的数据中,coordinates是list. list因此,以这种方式定义您的 Geometry 类:@Getter@NoArgsConstructor(access = AccessLevel.PROTECTED)@ToStringpublic class Geometry {&nbsp; &nbsp; private String type;&nbsp; &nbsp; private List<Coordinate> coordinates;}希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java