2017-03-20 26 views
0

我試圖從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給用戶,或者我可以實現嗎?

回答

2

這裏user不是array..it是一個JSONObject

試試這個:

JSONObject User = c.getJSONObject("user"); 

String id = User.getString("id"); 
String first_name = User.getString("first_name"); 

或修改您的JSON響應返回數組來代替。

此外您的JSON響應無效。

這是有效的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" //remove comma here 
     } 
    }] 
} 

你可以從HERE

+0

我用這個,但仍然無法獲得用戶值。 – user1058652

+0

您是否驗證了您的JSON?它會工作,如果JSON是有效的..你的用戶對象包含額外的逗號... – rafsanahmad007

+0

我想我有一個問題,別的,,, – user1058652

1

你必須申請嵌套結構類似這樣的

 JSONArray arrUser1 = c.getJSONArray("user");    
     for (int j = 0; j < arrUser1.length(); j++) { 
      JSONObject c1 = arrUser1.getJSONObject(j); 
      name = c1.getString("first_name"); 
      phone = c1.getString("telephone"); 

      mapItems = new HashMap<>();     
      mapItems.put("id", id); 
      mapItems.put("first_name", name); 
      mapItems.put("comment_text", commentText); 
      mapItems.put("telephone", phone); 
      contactList.add(mapItems); 

      } 
+0

檢查有效的JSON我以前試過,但在'mapItems'不能達到第二串For循環。 – user1058652

+0

你得到的名字和手機的價值意味着檢查你的HashMap初始化代碼 – sasikumar

+0

'HashMap mapItems;' – user1058652

1

用戶是的JSONObject在這種情況下。我不知道爲什麼你使用了JSONArray

這應該夠了。

JSONObject user = c.getJSONObject("user"); 
name = user.getString("first_name"); 
phone = user.getString("telephone"); 
+0

你不能定義'JSONObject'並且得到'c.getJSONArray'這是非法的! – user1058652

+0

我以爲我糾正了這一點。我的錯誤 –