【问题标题】:How to get json object in android?如何在android中获取json对象?
【发布时间】:2017-02-07 10:45:53
【问题描述】:

我正在开发一个应用程序,我想在其中获取一些 json 格式的数据,但我不知道如何获取。请指导我。

Json 数据:-

{
    "title":"title",
        "description":"description",
        "icon":{
    "url":"<image-url>",
            "aspectRatio":1,
            "height":75,
            "width":75
},
    "screenshots":{
    "url":"<image-url>",
            "aspectRatio":1.91,
            "height":627,
            "width":1200
},
    "landingURL":"LandingUrl",
        "cta":"cta",
        "rating":"rating"
}

【问题讨论】:

标签: android


【解决方案1】:

将您的响应放入 Jsonobject 中。

 JSONObject jsonObject = new JSONObject(response);

并解析您的 json。

【讨论】:

    【解决方案2】:
    JSONObject jsonObject = new JSONObject(data);
    String title = jsonObject.getString("title");
    String description = jsonObject.getString("description");
    JSONObject iconObject = jsonObject.JSONObject("icon");
    

    这是您解析 json 数据的方法。但最好使用Gson

    【讨论】:

      【解决方案3】:

      查看以下代码:

      try {
           JSONObject jsonObject = new JSONObject(response);
           String title = jsonObject.getString("title");
           String description = jsonObject.getString("description");
           JSONObject jObj = new JSONObject("icon");
               if(jObj.has("url")) {
                  String url = jObj.getString("url");
                  int aspectRatio = jObj.getInt("aspectRatio");
                  int height = jObj.getInt("height");
                  int width = jObj.getInt("width");
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-16
        • 2016-11-16
        • 2022-01-24
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 1970-01-01
        • 2023-02-26
        相关资源
        最近更新 更多