我们如何获得这种json数据?

时间:2019-09-12 10:48:43

标签: android json kotlin

请帮助我解决此问题。

请参见下面的示例,我想在列表视图中获取此数据。我们该怎么做?

{"Afpak":{"id":1,"race":"hybrid","flavors":["Earthy","Chemical","Pine"],"effects":{"positive":["Relaxed","Hungry","Happy","Sleepy"],"negative":["Dizzy"],"medical":["Depression","Insomnia","Pain","Stress","Lack of Appetite"]}},"African":{"id":2,"race":"sativa","flavors":["Spicy/Herbal","Pungent","Earthy"],"effects":{"positive":["Euphoric","Happy","Creative","Energetic","Talkative"],"negative":["Dry Mouth"],"medical":["Depression","Pain","Stress","Lack of Appetite","Nausea","Headache"]}}}

enter image description here

3 个答案:

答案 0 :(得分:1)

以上是JsonObject,但您需要一个JsonArray才能在列表视图中对其进行解析。

[{"id":1,"race":"hybrid","flavors":["Earthy","Chemical","Pine"],"effects":{"positive":["Relaxed","Hungry","Happy","Sleepy"],"negative":["Dizzy"],"medical":["Depression","Insomnia","Pain","Stress","Lack of Appetite"]}},{"id":2,"race":"sativa","flavors":["Spicy/Herbal","Pungent","Earthy"],"effects":{"positive":["Euphoric","Happy","Creative","Energetic","Talkative"],"negative":["Dry Mouth"],"medical":["Depression","Pain","Stress","Lack of Appetite","Nausea","Headache"]}}]

请修复您的json,并在api调用中将其解析为Jsonarray

答案 1 :(得分:1)

您可以通过一种简单的方式进行解析,首先,您必须安装AwesomePojoGenerator。 使用AwesomePojoGenerator从上述json生成模型类,然后使用Google的GSON库进行解析。

OutputClass outputClass=new Gson().fromJson(jsonString,OutputClass.class)

答案 2 :(得分:0)

您有没有要显示的列表视图模板,因为您无法在列表视图的常规迭代中显示这些值,因为在json数组上进行迭代并且json对象获取数据不同。