【问题标题】:How to display json data into the android layout?如何将json数据显示到android布局中?
【发布时间】:2017-12-11 10:43:24
【问题描述】:

我的 JSON 看起来像这样。我只需要在片段的布局中设置这些值。以便显示有关此产品的详细信息。



{
  "adid": 5,
  "uid": 1130,
  "userName": "Maëly",
  "cid": 5,
  "brand": null,
  "model": null,
  "title": "Nummular dermatitis",
  "description": "Nam sa dea ss "
  "address": "9795 Debra Center",
  "price": 1544,
  "photo1": "../../../img/mobile/oneplus/3t/3t-1.jpg",
  "photo2": null,
  "photo3": null,
  "photo4": null,
  "ip_add": null,
  "created": 1493921700000,
  "expiryDate": null,
  "published": true,
  "sold": null,
  "featured": true,
  "cond": true,
  "price_neg": true,
  "used_for": "8",
  "views": 1575,
  "city": "Kathmandu",
  "landmark": null,
  "quality": null,
  "alert": null,
  "features": [

  ]
}

【问题讨论】:

  • 添加您尝试过的代码。
  • 请参考以下链接获取帮助json to refrofit conversion
  • MuthuKrishnan 先生,我有一个 pojo 类、ApiInterface 和 ApiClient 以及一个带有片段活动的空白片段来显示结果。我添加了一些代码。

标签: android android-studio android-fragments retrofit retrofit2


【解决方案1】:

做 SpaceBison 建议的步骤 然后这样做, 如果例如JSONSCHEMATOPOJO 为您创建类 POJO 然后执行此操作。

try{
JSONObject object = new JSONObject(string);
Gson gson= new Gson();
POJO p = gson.fromJson(object,POJO.class)'
}catch(JSONException){
e.printStackStra();
}

【讨论】:

    【解决方案2】:

    看过改造文档吗? http://square.github.io/retrofit/?

    您可以对服务进行同步或异步调用,为您的界面创建并随意处理结果。如果您希望在片段中显示数据,您可能会进行异步调用,当您取回数据时(通过 Gson 转换器应自动将其从 JSON 转换为您的数据模型类),您可以例如在 ListView 或其他内容中显示数据对象列表。

    【讨论】:

      【解决方案3】:

      我建议使用a Gson converter。您可以使用 jsonschema2pojo 生成 Gson POJO 定义 - 尽管名称它也可以使用示例 JSON。

      【讨论】:

      • 我已经做到了,现在我很困惑如何使用那个 Json。我不知道下一步该做什么。
      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 2021-04-27
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2011-06-07
      相关资源
      最近更新 更多