Deserializing json array using gson

前端 未结 4 1419
深忆病人
深忆病人 2020-12-16 18:34

Continuing from this question.

I\'m having trouble deserializing the following json array (Sorry for the size):

\"geometry\": { \"type\": \"Polygon\"         


        
4条回答
  •  情话喂你
    2020-12-16 19:35

    The coordinates in JSON is a three-dimensional matrix. With Collection[][] you're going one dimension too far. The Collection itself is already one dimension, so you've basically declared a four-dimensional matrix.

    With the error message, Gson is basically telling you that it is expecting an object for the fourth dimension, but it instead encountered a double.

    The following represent valid three-dimensional matrices which should be perfectly handled by Gson:

    • private double[][][] coordinates; (recommended)
    • private Collection[] coordinates;
    • private Collection coordinates;
    • private Collection> coordinates;
    • private Collection>> coordinates;

    That said, I would prefer List above Collection in this particular case. With a List you can guarantee that it's been filled with insertion order and you'll be able to get elements by index.

提交回复
热议问题