使用Jackson解析JSON数组中没有键的数组对象列表

时间:2016-06-15 09:46:24

标签: java android arrays json jackson

我正在尝试解析在JSON数组中定义的数组列表。但我面临的问题是JSON数组中的数组列表没有任何键。

我在运行代码时遇到此异常: -

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.myapp.dto.WayPoints out of START_ARRAY token at [Source:retrofit.ExceptionCatchingTypedInput$ExceptionCatchingInputStream@42d3464; line: 1, column: 6996]

这是我的JSON的样子: -

{"route": {
  "waypoints": [
    [
      35.407744,
      139.463554
    ],
    [
      35.420147,
      139.52120800000003
    ],
    [
      35.461081,
      139.59132
    ],
    [
      35.471748,
      139.71372
    ],
    [
      35.304347,
      139.778226
    ],
    [
      35.189787,
      139.7631
    ],
    [
      35.139229,
      139.722619
    ],
    [
      35.115603,
      139.673938
    ],
    [
      28.205743000000002,
      131.958977
    ],
    [
      21.2065575,
      123.78503210000001
    ],
    [
      19.7573453,
      121.4626719
    ],
    [
      19.2394425,
      120.6539168
    ],
    [
      18.886745,
      120.1670009
    ],
    [
      18.4292838,
      119.87970600000001
    ],
    [
      17.8987841,
      119.78046750000001
    ],
    [
      17.368127,
      119.52847100000001
    ],
    [
      1.3890885,
      104.4579719
    ],
    [
      1.3054225,
      104.31617560000001
    ],
    [
      1.2450838,
      104.1318135
    ],
    [
      1.182028,
      103.7672242
    ],
    [
      1.2051203,
      103.531714
    ],
    [
      1.247416,
      103.3209176
    ],
    [
      1.4055484,
      102.9089111
    ],
    [
      2.661926,
      101.193657
    ],
    [
      6.7192953,
      95.6767995
    ],
    [
      7.3299653000000005,
      94.4741965
    ],
    [
      7.977403400000001,
      92.167991
    ],
    [
      8.030722400000002,
      90.7713732
    ],
    [
      7.8187621,
      88.2752215
    ],
    [
      6.3926252,
      83.5418168
    ],
    [
      5.819811,
      82.2456031
    ],
    [
      5.660262300000001,
      81.0391513
    ],
    [
      6.3259034,
      75.473531
    ],
    [
      9.932762,
      62.786145
    ],
    [
      12.9051102,
      49.5155532
    ],
    [
      12.7489972,
      46.9425057
    ],
    [
      12.190752,
      44.273983
    ],
    [
      12.230134,
      43.934253
    ],
    [
      12.479082,
      43.423469
    ],
    [
      13.148656,
      43.19157400000001
    ],
    [
      14.19765,
      42.513959
    ],
    [
      16.3665,
      41.253825
    ],
    [
      20.357763,
      38.033496
    ],
    [
      25.091396,
      35.574714
    ],
    [
      26.6703334,
      34.6287713
    ],
    [
      27.551982,
      34.0345062
    ],
    [
      28.181951,
      33.42400200000001
    ],
    [
      28.960185,
      32.904215
    ],
    [
      29.594466000000004,
      32.607293
    ],
    [
      29.868436,
      32.541128
    ],
    [
      29.922119,
      32.554244
    ],
    [
      29.927432999999997,
      32.557997
    ],
    [
      29.946581,
      32.579434
    ],
    [
      29.958438,
      32.584565
    ],
    [
      29.970295000000004,
      32.586608
    ],
    [
      29.985722,
      32.58567
    ],
    [
      30.001148,
      32.582671
    ],
    [
      30.037798,
      32.575643
    ],
    [
      30.078476,
      32.570851
    ],
    [
      30.121084000000003,
      32.570347
    ],
    [
      30.201545999999997,
      32.56830500000001
    ],
    [
      30.235939000000002,
      32.542933
    ],
    [
      30.256682000000005,
      32.515487
    ],
    [
      30.283044,
      32.44837
    ],
    [
      30.305243000000004,
      32.412138
    ],
    [
      30.40989,
      32.358914
    ],
    [
      30.443259,
      32.352699
    ],
    [
      30.466884,
      32.34795
    ],
    [
      30.509518,
      32.338528
    ],
    [
      30.525562,
      32.329846
    ],
    [
      30.546778000000003,
      32.310344
    ],
    [
      30.559346000000005,
      32.305669
    ],
    [
      30.570986,
      32.303122
    ],
    [
      30.594747000000005,
      32.312249
    ],
    [
      30.615476000000005,
      32.322837
    ],
    [
      30.637239,
      32.328879
    ],
    [
      30.673821999999998,
      32.337275
    ],
    [
      30.695362,
      32.342289
    ],
    [
      30.706204999999997,
      32.344025
    ],
    [
      30.716311000000005,
      32.3437
    ],
    [
      30.785226,
      32.324031
    ],
    [
      30.814229,
      32.318051
    ],
    [
      30.855246,
      32.316787
    ],
    [
      30.92545,
      32.314211
    ],
    [
      30.995507,
      32.312062
    ],
    [
      31.064055,
      32.30973
    ],
    [
      31.124595,
      32.30731
    ],
    [
      31.221262999999997,
      32.304584
    ],
    [
      31.24915,
      32.306097
    ],
    [
      31.263020000000004,
      32.317584
    ],
    [
      31.272302999999997,
      32.325731
    ],
    [
      31.276450000000004,
      32.316541
    ],
    [
      31.670572,
      31.837068000000002
    ],
    [
      31.767885,
      30.811998
    ],
    [
      37.9593753,
      6.5972614
    ],
    [
      37.9452645,
      4.9190716
    ],
    [
      37.718478,
      3.4223181
    ],
    [
      36.757234,
      -0.5801307
    ],
    [
      36.2409795,
      -3.4370313
    ],
    [
      35.950637,
      -5.827275
    ],
    [
      36.0965299,
      -6.6596297
    ],
    [
      36.8398906,
      -9.0766174
    ],
    [
      37.5455464,
      -9.5894154
    ],
    [
      38.180763,
      -9.668073
    ],
    [
      40.9856727,
      -9.8365241
    ],
    [
      43.3549412,
      -9.469394
    ],
    [
      44.7745807,
      -8.0000582
    ],
    [
      47.013348,
      -6.433106
    ],
    [
      48.3603377,
      -5.3426872
    ],
    [
      49.4314004,
      -3.5484451
    ],
    [
      50.591437,
      0.996336
    ],
    [
      51.343279,
      2.373862
    ],
    [
      51.379888,
      2.760273
    ],
    [
      51.365058,
      3.188211
    ],
    [
      51.349485,
      3.20612
    ],
    [
      51.33683200000001,
      3.209724
    ],
    [
      51.2986682,
      3.5615743
    ],
    [
      51.2444631,
      3.9788076
    ],
    [
      51.223158,
      4.395356
    ],
    [
      51.2401821,
      5.1084064
    ],
    [
      51.18407,
      5.820852
    ],
    [
      51.292057,
      8.095025
    ],
    [
      51.973936,
      9.904372
    ],
    [
      52.189005,
      12.016328
    ],
    [
      52.435496,
      16.800789
    ],
    [
      51.991783,
      20.511753000000002
    ]
  ],
  "currentLocation": {
    "longitude": 139.463554,
    "latitude": 35.407744
  }
}
}

以下是我试图解析响应的类。

@JsonProperty("route")
public class Route {

    @JsonProperty("currentLocation")
    private OrderLocation currentLocation;

    @JsonProperty("waypoints")
    private List<WayPoints> wayPointsList;

    public OrderLocation getCurrentLocation() {
        return currentLocation;
    }

    public void setCurrentLocation(OrderLocation currentLocation) {
        this.currentLocation = currentLocation;
    }

    public List<WayPoints> getWayPointsList() {
        return wayPointsList;
    }

    public void setWayPointsList(List<WayPoints> orderWayPointsList) {
        this.wayPointsList = wayPointsList;
    }
}


public class WayPoints {

    private double lat;
    private double lng;

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }
}

我是杰克逊图书馆的新手,无法弄清楚这一点,我是否需要使用任何密钥来解析航点数据。

1 个答案:

答案 0 :(得分:0)

您可以为wayPointsList编写反序列化器:

public class WayPointsDeserializer extends JsonDeserializer<List<WayPoints>> {

    @Override
    public List<WayPoints> deserialize(JsonParser jsonParser, DeserializationContext ctxt) 
      throws IOException, JsonProcessingException {
        List<WayPoints> wayPoints = new ArrayList<>();
        if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
            while(jsonParser.nextToken() != JsonToken.END_ARRAY) {
                WayPoints wp = new WayPoints();
                jsonParser.nextToken();
                wp.setLat(jsonParser.getDoubleValue());
                jsonParser.nextToken();
                wp.setLng(jsonParser.getDoubleValue());
                wayPoints.add(wp);
                jsonParser.nextToken();
            }
            return wayPoints;
        }
        throw ctxt.mappingException("Expect '['");
    }
}

并使用:

public class Route {

    ....

    @JsonProperty("waypoints")
    @JsonDeserialize(using = WayPointsDeserializer.class)
    private List<WayPoints> wayPointsList;

    .....
}