2016-01-12 150 views
0

所以我是一般的json解析的新手。在社區的幫助下,我能夠讓json迴歸。但是,我的生活無法弄清楚如何訪問/獲得某些價值。我試圖在「內容」部分中獲取/訪問「halfjpeg」值。從返回的json獲取價值

[ 
    { 
    "publicId": "1337", 
    "name": "mypainting", 
    "orientation": "vertical", 
    "provider": "user", 
    "providerFullName": "unknown", 
    "location": { 
     "coordinates": [ 
     -71, 
     42 
     ], 
     "userCityCode": "BOS", 
     "userCityName": "Boston", 
     "userStateCode": "MA", 
     "userStateName": "Massachusetts", 
     "userCountryCode": "US", 
     "userCountryName": "United States", 
     "userZipCode": "02108", 
     "latitude": 42 
     "longitude": -71 
    }, 
    "status": { 
     "isLive": false, 
     "isCertified": false, 
     "hasVirusWarning": true 
    }, 
    "content": { 
     "halfJpeg": "userhalfpaintlocation.com", 
     "fullJpeg": "userfullpaintlocation.com", 
     "hugeJpeg": "userhugepaintlocation.com" 
    }, 
    "createdAt": xxxxxxxx, 
    "updatedAt": xxxxxxxx, 
    "policy": { 
     "isAdmin": false, 
     "isUser": true 
    } 
    } 
] 

我的代碼來獲得JSON低於:

URL url = null; 
     try { 
      url = new URL("getthepaintingurl.com"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     HttpURLConnection conn = null; 
     try { 
      conn = (HttpURLConnection) url.openConnection(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      conn.setRequestMethod("GET"); 
     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } 

     try { 
      System.out.println("Response Code: " + conn.getResponseCode()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     InputStream in = null; 
     try { 
      in = new BufferedInputStream(conn.getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     StringBuilder out = new StringBuilder(); 
     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       out.append(line + "\n"); 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println(out.toString()); 

     try { 
      JSONObject jObject = new JSONObject(out.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+0

你有沒有嘗試過谷歌一點點前問一個問題更多的文檔? –

+0

你從一個'JSONArray'的外觀開始:這裏是你需要知道的一切:http://docs.oracle.com/javaee/7/api/javax/json/package-summary.html – Adam

+0

Yup ,我試着解析自己,但似乎無法正確訪問正確的節點/孩子。 – AndroidDev21921

回答

1

http://www.w3schools.com/json/json_syntax.asp

只要你知道一些語法,看了上面的(它應該有助於澄清!)。

你所期待的迴應是以JSONArray的形式。因此,這將是這個樣子:

//create json array from the buffer string, and get the inner json object 
JSONObject response = new JSONArray(out.toString()).getJSONObject(0); 

//get the json object at key "content" 
JSONObject content = response.getJSONObject("content"); 

//access the value of "halfJpg" 
String halfJpgSrc = content.getString("halfJpeg"); 

評論應該解釋自己,但你基本上是從你的要求輸出創建一個JSONArray,並訪問其中包含的所有信息(索引0)內JSON對象。然後,您只需導航到您發送的數據。在這種情況下,內部數據存儲在鍵/值對中,所以它應該是非常明顯的,如何訪問JSON響應中的任何其他內容。

下面是這兩個對象(JSONArray/JSONObject的)

JSONObjectJSONArray

+0

盧卡斯 - 非常感謝你!這個解釋非常有幫助和徹底。我之前的問題是我將索引[0]分配給'內容'而不是響應對象。再次感謝! – AndroidDev21921

+0

ahhh,很高興我可以幫忙!沒問題! –

+0

盧卡斯 - 只是想再次感謝你。已經過了一個星期左右的時間,但想讓你知道你的解釋和答案幫助我更好地掌握JSON對象。 – AndroidDev21921