【问题标题】:How can we get this type of json data?我们怎样才能得到这种类型的 json 数据呢?
【发布时间】:2019-09-12 10:48:43
【问题描述】:

请帮我解决这个问题。

看下面这个例子,我想在列表视图中获取这些数据。我们该怎么做?

{"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"]}}}

【问题讨论】:

标签: android json kotlin


【解决方案1】:

您可以轻松地进行解析,首先您必须安装AwesomePojoGenerator。 使用AwesomePojoGenerator将上述json生成模型类,然后使用google的GSON库进行解析。

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

【讨论】:

    【解决方案2】:

    您是否有任何列表视图模板要显示,因为您无法在列表视图中的正常迭代中显示这些值,因为迭代 json 数组和 json 对象与获取数据不同。

    【讨论】:

      【解决方案3】:

      上面这个是 JsonObject 但你需要一个 JsonArray 来在 listview 中解析它。

      [{"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

      【讨论】:

      • jsoneditoronline.org 过去这个 url 中的 json 并检查它是否给出错误
      • 我已经编辑了 json ,你需要像上面这样的 jsonarray 。你必须管理你的领域,特别是 JsonObject。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多