I am lost
首先,我想告诉您,在 androidhive 的示例中,jsonstring 的root
元素是 ,而您作为问题发布的 jsonstringjsonobject
的元素是. 其次,不必总是在数组的开头有一个名称来使用它并从中提取数据。但是,在复杂的情况下肯定需要它,这也是一种很好的做法。root
jsonarray
How can I parse this data?
根元素不同,所以需要改变消费数据的方式,
以 androidhive 为例,
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url); //look at the left side of assignment operator.here result is being consumed in JSONObject
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
}
} catch (JSONException e) {
e.printStackTrace();
}
您作为问题发布的 jsonstring 可以在 JSONArray 中使用,
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url); //look at the left side of assignment operator.here result is being consumed in JSONArray
try {
// looping through All data
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// Storing each item in variable
String id = c.getString("id");
String title= c.getString("title");
String permalink= c.getString("permalink");
String content= c.getString("content");
}
} catch (JSONException e) {
e.printStackTrace();
}