您必須爲此使用JSONObject
。下面的代碼使用 -
JSONParser的.java
public JSONObject getJSONFromUrl(String url) // url is your json url
{
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
它會返回JsonObject
並利用該jsonbobject我們可以得到的結果作爲數組。
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
for(int i = 0; i < contacts.length(); i++) {
String product_name = contacts.getString("productname");
String image_url = contacts.getString("imageurl");
.....
.....
String store_name = contacts.getString("storename");
}
}catch(JSONException e)
{
e.printStackTrace();
}
更多隻是指Android json tutorial
具有u嘗試,直到又是什麼? –
https://www.google.com/search?q=android+parse+json – jolivier
請參閱[this](http://www.androidhive.info/2012/01/android-json-parsing-tutorial/)以及,看看那裏的圖片鏈接。他們如何使用JSONObject獲取json值。 – Praveenkumar