2016-01-12 115 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