我試圖從Json的下面一樣檢索數據:我如何從我的AsyncTask訪問此Json?
{
comments: [
{
id: 78,
comment_user_id: 81,
comment_is_approve: 1,
comment_ads_id: 373,
comment_text: "commmmmmeeeent here ",
created_at: "2017-03-19 08:32:17",
updated_at: "2017-03-19 08:32:17",
user: {
id: 81,
first_name: "name",
last_name: "",
age: "",
email: "[email protected]",
telephone: "234234234",
}
}
]
}
這裏是我asyncTask()
:
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url + 373, "GET", params);
// Check your log cat for JSON reponse
//Log.d("All Comments: ", json.toString());
try {
JSONArray comments = json.getJSONArray("comments");
// looping through All Comments
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
String commentText = c.getString("comment_text");
String name = "";
String phone = "";
Log.i(TAG, "doInBackground Items: " + id + " , "+ commentText);
//Loop through All user details
JSONArray arrUser = c.getJSONArray("user");
int l = 0;
JSONObject user = arrUser.getJSONObject(l++);
name = user.getString("first_name");
phone = user.getString("telephone");
// creating new HashMap
Log.i(TAG, "doInBackground Items: " + name +", " + commentText + ", " + phone);
// adding HashList to ArrayList
mapItems = new HashMap<>();
// adding each child node to HashMap key => value
mapItems.put("id", id);
mapItems.put("first_name", name);
mapItems.put("comment_text", commentText);
mapItems.put("telephone", phone);
contactList.add(mapItems);
我能得到commentText和id,但我不能從任何數據用戶數組? 我應該添加parantethes給用戶,或者我可以實現嗎?
我用這個,但仍然無法獲得用戶值。 – user1058652
您是否驗證了您的JSON?它會工作,如果JSON是有效的..你的用戶對象包含額外的逗號... – rafsanahmad007
我想我有一個問題,別的,,, – user1058652