简体   繁体   English

使用 GSON 解析复杂 json android

[英]Parsing complex json android using GSON

I have json file like我有 json 文件,例如

    {
        "type": "Feature",
        "properties": {
          "centlat": -20.63101971,
          "ICAOCODE": "FMMM",
          "REGION": "AFI",
          "FIRname": "FIR ANTANANARIVO",
          "centlong": 48.56534436,
          "StateCode": "MDG",
          "StateName": "Madagascar"
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                57.00000190700007,
                -29.999999999999943
              ],
              [
                57.00000190700007,
                -22.333332061999954
              ],
              [
                55.50000190700007,
                -18.999999999999943
              ],
              [
                55.50000190700007,
                -9.999999999999943
              ],
              [
                45.00000190800006,
                -9.999999999999943
              ],
              [
                44.00000190800006,
                -10.333332060999965
              ],
              [
                41.50000190700007,
                -10.999999999999943
              ],
              [
                43.00000190700007,
                -14.999999999999943
              ],
              [
                40.00000190800006,
                -19.999999999999943
              ],
              [
                40.00000190800006,
                -29.999999999999943
              ],
              [
                57.00000190700007,
                -29.999999999999943
              ]
            ]
          ]
        }
      }

I have installed the plugin robo pogo generator which gave me this output

package com.example.airlineapp.model

import com.google.gson.annotations.SerializedName

data class GeographicalData(

    @field:SerializedName("geometry")
    val geometry: Geometry? = null,

    @field:SerializedName("type")
    val type: String? = null,

    @field:SerializedName("properties")
    val properties: Properties? = null
)

data class Geometry(

    @field:SerializedName("coordinates")
    val coordinates: ArrayList<Double?>? = null,

    @field:SerializedName("type")
    val type: String? = null
)

data class Properties(

    @field:SerializedName("centlong")
    val centlong: Double? = null,

    @field:SerializedName("centlat")
    val centlat: Double? = null,

    @field:SerializedName("FIRname")
    val fIRname: String? = null,

    @field:SerializedName("StateName")
    val stateName: String? = null,

    @field:SerializedName("StateCode")
    val stateCode: String? = null,

    @field:SerializedName("ICAOCODE")
    val iCAOCODE: String? = null,

    @field:SerializedName("REGION")
    val rEGION: String? = null
)

I tried parsing the code but it always shows Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 16 column 10 path $[0].geometry.coordinates[0]我尝试解析代码,但它总是显示Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 16 column 10 path $[0].geometry.coordinates[0]

I rectified the issue but the thing is how can I parse this json now with a complex array structure, any suggesstions?我纠正了这个问题,但问题是我现在如何用复杂的数组结构解析这个 json,有什么建议吗?

I also tried adding Array of arrays in my POJO classs similar to the struture of json file but that did not worked.我还尝试在我的 POJO 类中添加 arrays 数组,类似于 json 文件的结构,但这没有用。

The coordinate array is basically an array of array of arrays, a three level array坐标数组基本上是arrays的数组,一个三级数组

Your JSON structure is wrong, instead of this你的 JSON 结构是错误的,而不是这个

...
 "coordinates": [
            [
              [
                57.00000190700007,
                -29.999999999999943
              ],
...

you should have this你应该有这个

...
 "coordinates": [
              {
                57.00000190700007,
                -29.999999999999943
              },
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM