如何使用Gson解析没有keyName的JSON数组?

时间:2016-02-18 21:05:05

标签: java android json gson

我有下一个JSON:

{
    "type": "FootprintCollection",
    "footprints": [
        {
            "type": "Footprint",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                35.6893257989465,
                                50.3346798773745
                            ],
                            [
                                35.6907727397702,
                                50.3383230660567
                            ],
                            [
                                35.692323032482,
                                50.3446275905909
                            ],
                            [
                                35.696663852255,
                                50.3455577665777
                            ],
                            [
                                35.7100480485786,
                                50.3559964053099
                            ],
                            [
                                35.7210034521477,
                                50.369070543271
                            ],
                            [
                                35.7374365579508,
                                50.3819379747578
                            ],
                            [
                                35.7459114928212,
                                50.384315090669
                            ],
                            [
                                35.7651351256856,
                                50.3848835314499
                            ],
                            [
                                35.7740751485493,
                                50.387260647361
                            ],
                            [
                                35.7852372576933,
                                50.394857083088
                            ],
                            [
                                35.8051326841261,
                                50.4125304223411
                            ],
                            [
                                35.8190853212307,
                                50.4194033876562
                            ],
                            [
                                35.8371720725332,
                                50.4235891786977
                            ],
                            [
                                35.9315849136129,
                                50.4305396592779
                            ],
                            [
                                36.1056311381665,
                                50.4214704452049
                            ],
                            [
                                36.1330196466394,
                                50.4119619815602
                            ],
                            [
                                36.1404610536348,
                                50.3933067894768
                            ],
                            [
                                36.169089796457,
                                50.3840308698288
                            ],
                            [
                                36.2687736341959,
                                50.281737372182
                            ],
                            [
                                36.2917179701083,
                                50.2743993188735
                            ],
                            [
                                36.3156441588509,
                                50.2752261420728
                            ],
                            [
                                36.3392086114883,
                                50.2818407249695
                            ],
                            [
                                36.3611194195257,
                                50.2919693062386
                            ],
                            [
                                36.3807047866966,
                                50.3044233265753
                            ],
                            [
                                36.3876811056985,
                                50.3068004424865
                            ],
                            [
                                36.3998767436169,
                                50.3069554712181
                            ],
                            [
                                36.4097469415681,
                                50.3049400905128
                            ],
                            [
                                36.4303141615694,
                                50.2972403019984
                            ],
                            [
                                36.4524316751817,
                                50.2939330101004
                            ],
                            [
                                36.4799752132855,
                                50.2812722841886
                            ],
                            [
                                36.5118595720617,
                                50.2768022732064
                            ],
                            [
                                36.520747918082,
                                50.2744509957169
                            ],
                            [
                                36.5361474951109,
                                50.267913927186
                            ],
                            [
                                36.5386279638095,
                                50.2654076200657
                            ],
                            [
                                36.5358374367484,
                                50.261247667446
                            ],
                            [
                                36.5337187032556,
                                50.2390009626244
                            ],
                            [
                                36.5313932632885,
                                50.2291824406172
                            ],
                            [
                                36.5344421727681,
                                50.2203199330186
                            ],
                            [
                                36.549221633072,
                                50.212542629239
                            ],
                            [
                                36.5707190281607,
                                50.2072457950575
                            ],
                            [
                                36.5940767761224,
                                50.2047911647805
                            ],
                            [
                                36.6166076999854,
                                50.2067548695417
                            ],
                            [
                                36.6355729513307,
                                50.2144029812127
                            ],
                            [
                                36.6440995621451,
                                50.2226195336646
                            ],
                            [
                                36.660119255899,
                                50.2442719592835
                            ],
                            [
                                36.6695760435996,
                                50.2533670108789
                            ],
                            [
                                36.682391798243,
                                50.2609634466058
                            ],
                            [
                                36.8484281759197,
                                50.3240862090114
                            ],
                            [
                                36.9137471861837,
                                50.3394082707752
                            ],
                            [
                                36.9797896668597,
                                50.3424830186764
                            ],
                            [
                                37.0514648785014,
                                50.3356875677271
                            ],
                            [
                                37.1966756528591,
                                50.3627660178375
                            ],
                            [
                                37.2263896012991,
                                50.3774421253539
                            ],
                            [
                                37.2848873230915,
                                50.4156051702425
                            ],
                            [
                                37.3144462419006,
                                50.4233307971786
                            ],
                            [
                                37.4150085796823,
                                50.4322191431989
                            ],
                            [
                                37.4350590348466,
                                50.4252945010405
                            ],
                            [
                                37.449941847938,
                                50.399404608436
                            ],
                            [
                                37.4635327489373,
                                50.3669001329349
                            ],
                            [
                                37.479707472322,
                                50.340390122706
                            ],
                            [
                                37.567867465711,
                                50.312872423024
                            ],
                            [
                                37.5989250021872,
                                50.2911941598827
                            ],
                            [
                                37.6012504412549,
                                50.2494137638342
                            ],
                            [
                                37.5912252136727,
                                50.2256684431442
                            ],
                            [
                                37.5883313329247,
                                50.2149714219936
                            ],
                            [
                                37.5900366552675,
                                50.2041710471561
                            ],
                            [
                                37.5969096205826,
                                50.197246405897
                            ],
                            [
                                37.6316361832633,
                                50.1744312611938
                            ],
                            [
                                37.7274442892226,
                                50.0790107288621
                            ],
                            [
                                37.7631010269909,
                                50.0629393582648
                            ],
                            [
                                37.8425277039904,
                                50.0394782575156
                            ],
                            [
                                37.8813883808691,
                                50.0233552109743
                            ],
                            [
                                37.915598178713,
                                50.0036148141724
                            ],
                            [
                                37.9462423031399,
                                49.9785517438679
                            ],
                            [
                                37.9786951026969,
                                49.9417839629596
                            ],
                            [
                                37.9796769546278,
                                49.9399494494076
                            ],
                            [
                                37.9950765316567,
                                49.9217076684742
                            ],
                            [
                                37.9955932964935,
                                49.9062822539229
                            ],
                            [
                                38.0001924986849,
                                49.9002102724862
                            ],
                            [
                                38.0228267762348,
                                49.9095895449216
                            ],
                            [
                                38.0242220393157,
                                49.9031558291782
                            ],
                            [
                                38.0274776543703,
                                49.8938540720079
                            ],
                            [
                                38.0434973490235,
                                49.8693594424843
                            ],
                            [
                                38.0459778177222,
                                49.8666205922666
                            ],
                            [
                                38.0518689311064,
                                49.8613237571857
                            ],
                            [
                                38.0552795757918,
                                49.858843288487
                            ],
                            [
                                38.0593103372024,
                                49.8566470406285
                            ],
                            [
                                38.0789990580601,
                                49.8490506049015
                            ],
                            [
                                38.0808594091345,
                                49.8462600778404
                            ],
                            [
                                38.0800842627785,
                                49.8431078155733
                            ],
                            [
                                38.0754333837437,
                                49.8399038764628
                            ],
                            [
                                38.0704724463464,
                                49.8381468772766
                            ],
                            [
                                38.0647363625931,
                                49.8373200549766
                            ],
                            [
                                38.0426705258242,
                                49.8361831734148
                            ],
                            [
                                38.0228267762348,
                                49.8324107935234
                            ],
                            [
                                38.0053084657132,
                                49.8264938417175
                            ],
                            [
                                38.000037469054,
                                49.8226439483596
                            ],
                            [
                                37.9984871763422,
                                49.8203701852359
                            ],
                            [
                                37.9975570003554,
                                49.8174504660662
                            ],
                            [
                                37.9997274106915,
                                49.8139623070149
                            ],
                            [
                                38.0015877617658,
                                49.8121794703064
                            ],
                            [
                                38.0434973490235,
                                49.7907079127401
                            ],
                            [
                                38.0453577000978,
                                49.7878657088355
                            ],
                            [
                                38.0451509945228,
                                49.7848167993558
                            ],
                            [
                                38.0410168803248,
                                49.7815611843013
                            ],
                            [
                                38.0366243837084,
                                49.7793132595993
                            ],
                            [
                                38.0234468929598,
                                49.7742748069368
                            ],
                            [
                                38.0197261899118,
                                49.7718460150816
                            ],
                            [
                                38.0164705748572,
                                49.7690554880204
                            ],
                            [
                                38.013680047796,
                                49.765696519279
                            ],
                            [
                                38.0113546078289,
                                49.761975816231
                            ],
                            [
                                38.0093392271237,
                                49.7577383492455
                            ],
                            [
                                38.0048433777197,
                                49.7505036487245
                            ],
                            [
                                38.0009676450408,
                                49.7495217958942
                            ],
                            [
                                37.9980220883488,
                                49.7494184431067
                            ],
                            [
                                37.9739408708746,
                                49.7532424989422
                            ],
                            [
                                37.9686698751147,
                                49.7527257350047
                            ],
                            [
                                37.9644840840733,
                                49.7510204126619
                            ],
                            [
                                37.9609184097569,
                                49.7485916208067
                            ],
                            [
                                37.9542521500168,
                                49.743010565785
                            ],
                            [
                                37.9421598648859,
                                49.7305048695042
                            ],
                            [
                                37.9399894545497,
                                49.7269391951878
                            ],
                            [
                                37.9393693378248,
                                49.7226500513588
                            ],
                            [
                                37.9421598648859,
                                49.717379055599
                            ],
                            [
                                37.9460355975648,
                                49.7147693956911
                            ],
                            [
                                37.9506864765997,
                                49.714485174851
                            ],
                            [
                                37.9776615739225,
                                49.7218749050029
                            ],
                            [
                                37.9829325705817,
                                49.7223916689404
                            ],
                            [
                                37.9882035663416,
                                49.7218749050029
                            ],
                            [
                                37.9929061213205,
                                49.7201954210819
                            ],
                            [
                                37.9960067076435,
                                49.7178441435924
                            ],
                            [
                                37.9983321467113,
                                49.715544542047
                            ],
                            [
                                38.0001924986849,
                                49.7126764997207
                            ],
                            [
                                38.0006575866784,
                                49.7111262070089
                            ],
                            [
                                38.0011226746718,
                                49.7084390327352
                            ],
                            [
                                37.9992623226981,
                                49.6806629506347
                            ],
                            [
                                37.9997274106915,
                                49.6684414742946
                            ],
                            [
                                38.0009676450408,
                                49.6638681096256
                            ],
                            [
                                38.0011226746718,
                                49.6605866561493
                            ],
                            [
                                38.0003475274165,
                                49.6565042178954
                            ],
                            [
                                37.9983321467113,
                                49.6520083684915
                            ],
                            [
                                37.9929061213205,
                                49.6462722847382
                            ],
                            [
                                37.9807621602455,
                                49.637073879456
                            ],
                            [
                                37.97735151556,
                                49.6323713244771
                            ],
                            [
                                37.9683598158529,
                                49.614594631537
                            ],
                            [
                                37.9637089377173,
                                49.6094786645088
                            ],
                            [
                                37.9589030290516,
                                49.6061455350884
                            ],
                            [
                                37.9505314469687,
                                49.6033550071279
                            ],
                            [
                                37.9471208022832,
                                49.6016755232069
                            ],
                            [
                                37.9457255392023,
                                49.5998668480767
                            ],
                            [
                                37.944485304853,
                                49.5967921001753
                            ],
                            [
                                37.9458805679339,
                                49.5913919132063
                            ],
                            [
                                37.9522367693115,
                                49.5772842473702
                            ]
                        ]
                    ]
                ]
            },
            "properties": {
                "id": "14128565",
                "custodian": "Natural Earth"
            }
        }
    ],
    "totalResults": "1"
}

我正在为coordinates创建POJO而堆叠。

public class Geometry {

    String type;

    Coordinates coordinates;
}

public class Coordinates {

    // What need to be here??? 
}

我被卡住了,因为我没有理解我如何解析coordinates标签。我怎样才能正确实现POJO的结构?

enter image description here

2 个答案:

答案 0 :(得分:1)

你不会看到钥匙,因为钥匙不是钥匙,而是价值。 (我真的没有看到你在坐标中拥有嵌套数组的意义。)

要解析coordinate,您将拥有类似的内容

public class Geometry{

    private ArrayList<ArrayList<ArrayList<Double[]>>> coordinates;
    //create getter and setter methods

}

解析它,

// Get Geometry object (however you do it)
// Then use the getter method to get coordinates variable
// Parse the nested arrays

ArrayList<Double[]> values = geometry.getCoordinates().get(0).get(0).get(0);

// You can now access the values, e.g.
Double[] firstArray = values.get(0);

注意:我可能遗失或超过ArrayList。请仔细检查一下。

答案 1 :(得分:0)

public class Coordinates {
    float latitude;
    float longitude; 
}

public class CoordinatesList1 {
    ArrayList<Coordinates> list1;
}

public class CoordinatesList2 {
    ArrayList<CoordinatesList1> list2;
}

public class Geometry {

    String type;

    ArrayList<CoordinatesList2> coordinates;
}

Gson gson = new Gson();
CoordinatesList2[] myTypes = gson.fromJson("input.json", Geometry.class);