【问题标题】:Using Retrofit and Gson with an api that returns dynamic top level JSON将 Retrofit 和 Gson 与返回动态顶级 JSON 的 api 一起使用
【发布时间】:2016-11-02 03:32:49
【问题描述】:

我正在使用 Retrofit 访问以下 api:

https://api.nasa.gov/neo/rest/v1/feed?api_key=DEMO_KEY

near_earth_objects 对象包含多个数组,每个数组都有一个表示日期的键。如果您访问不同的日期,此值显然会发生变化。

像往常一样,我根据返回的 JSON 结构定义了我的 POJO。以下是我的主要响应类:

public class AsteroidResponse {

    private Links links;

    @SerializedName("element_count")
    private Integer elementCount;

    @SerializedName("near_earth_objects")
    private NearEarthObjects nearEarthObjects;

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    //getters and setters
}

NearEarthObjects 类如下所示:

public class NearEarthObjects {

    private List<Observation> observation = new ArrayList<>();

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    //getters and setters
}

我之前遇到过这个问题,并且能够使用Map&lt;String, SomeCustomModel&gt; 让它自动解析并将日期设置为地图中的键。围绕 SO 的一些答案中建议使用此方法。

我尝试在这种情况下做同样的事情,将上述类替换为如下所示:

public class NearEarthObjects {

    private Map<String, Observation> observation = new HashMap<>();

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}

不幸的是,这次这种方法似乎没有按预期工作。地图返回为空。可能是什么问题?构建模型以正确解析返回的 JSON 的最佳方法是什么?

【问题讨论】:

    标签: java android json gson retrofit2


    【解决方案1】:

    我认为解析 Json 对象 near_earth_objects 中的数据的简单方法是这样返回:

    "near_earth_objects":[  
       {  
          "date":"2016-11-07",
          "data":[  
             {  
                "links":{  
                   "self":"https://api.nasa.gov/neo/rest/v1/neo/3758255?api_key=DEMO_KEY"
                },
                "neo_reference_id":"3758255",
                "name":"(2016 QH44)",
                "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3758255",
                "absolute_magnitude_h":22.381,
                "estimated_diameter":{  
                   "kilometers":{  
                      "estimated_diameter_min":0.0887881438,
                      "estimated_diameter_max":0.1985363251
                   },
                   "meters":{  
                      "estimated_diameter_min":88.7881437713,
                      "estimated_diameter_max":198.5363250687
                   },
                   "miles":{  
                      "estimated_diameter_min":0.0551703777,
                      "estimated_diameter_max":0.1233647148
                   },
                   "feet":{  
                      "estimated_diameter_min":291.2996936107,
                      "estimated_diameter_max":651.3659167384
                   }
                },
                "is_potentially_hazardous_asteroid":false,
                "close_approach_data":[  
                   {  
                      "close_approach_date":"2016-11-07",
                      "epoch_date_close_approach":1478505600000,
                      "relative_velocity":{  
                         "kilometers_per_second":"9.9505291907",
                         "kilometers_per_hour":"35821.9050865416",
                         "miles_per_hour":"22258.3387466903"
                      },
                      "miss_distance":{  
                         "astronomical":"0.1045395934",
                         "lunar":"40.6659011841",
                         "kilometers":"15638901",
                         "miles":"9717563"
                      },
                      "orbiting_body":"Earth"
                   }
                ]
             },
             {  
                "links":{  
                   "self":"https://api.nasa.gov/neo/rest/v1/neo/3758255?api_key=DEMO_KEY"
                },
                "neo_reference_id":"3758255",
                "name":"(2016 QH44)",
                "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3758255",
                "absolute_magnitude_h":22.381,
                "estimated_diameter":{  
                   "kilometers":{  
                      "estimated_diameter_min":0.0887881438,
                      "estimated_diameter_max":0.1985363251
                   },
                   "meters":{  
                      "estimated_diameter_min":88.7881437713,
                      "estimated_diameter_max":198.5363250687
                   },
                   "miles":{  
                      "estimated_diameter_min":0.0551703777,
                      "estimated_diameter_max":0.1233647148
                   },
                   "feet":{  
                      "estimated_diameter_min":291.2996936107,
                      "estimated_diameter_max":651.3659167384
                   }
                },
                "is_potentially_hazardous_asteroid":false,
                "close_approach_data":[  
                   {  
                      "close_approach_date":"2016-11-07",
                      "epoch_date_close_approach":1478505600000,
                      "relative_velocity":{  
                         "kilometers_per_second":"9.9505291907",
                         "kilometers_per_hour":"35821.9050865416",
                         "miles_per_hour":"22258.3387466903"
                      },
                      "miss_distance":{  
                         "astronomical":"0.1045395934",
                         "lunar":"40.6659011841",
                         "kilometers":"15638901",
                         "miles":"9717563"
                      },
                      "orbiting_body":"Earth"
                   }
                ]
             }
          ]
       }
     // The other objects 
    ]
    

    如果你想解析动态键。您可以使用此链接: How to parse a dynamic JSON key in a Nested JSON result?

    【讨论】:

    • 是的,它目前的结构是错误的,但我不拥有 API。在您提到的链接中的一个答案中,他们还尝试使用Map&lt;String, Obj&gt;,尽管如上所述,这似乎不适用于我的场景。我可以手动解析 JSON,但我希望 Gson 尽可能多地处理。
    • 您可以手动解析JSON,将JSON 设为字符串并使用JSONObject 进行解析。 [链接] (stackoverflow.com/questions/7304002/…)
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多