所以我是一般的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();
}
你有沒有嘗試過谷歌一點點前問一個問題更多的文檔? –
你從一個'JSONArray'的外觀開始:這裏是你需要知道的一切:http://docs.oracle.com/javaee/7/api/javax/json/package-summary.html – Adam
Yup ,我試着解析自己,但似乎無法正確訪問正確的節點/孩子。 – AndroidDev21921