2017-02-07 107 views
0

我正在開發一個應用程序,我想要獲取一些數據,這是在JSON格式,但我不知道如何獲取。請指導我。如何在android中獲取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" 
} 
+0

到目前爲止你已經嘗試過什麼? –

+0

可能的重複:http://stackoverflow.com/questions/5566669/how-to-parse-a-json-object-in-android –

回答

1

放入的JSONObject你的迴應。

JSONObject jsonObject = new JSONObject(response); 

並解析你的json。

0
JSONObject jsonObject = new JSONObject(data); 
String title = jsonObject.getString("title"); 
String description = jsonObject.getString("description"); 
JSONObject iconObject = jsonObject.JSONObject("icon"); 

這是你如何解析你的json數據。但使用Gson更好。

0

結帳下面的代碼:

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(); 
     } 
    }