我如何从我的AsyncTask访问这个Json?

时间:2017-03-20 09:40:01

标签: android json android-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: "l@mail.com",
              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还是我如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

这里user不是数组..它是一个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": "l@mail.com",
            "telephone": "234234234"    //remove comma here
        }
    }]
}

您可以从HERE

检查有效的JSON

答案 1 :(得分:0)

你必须像这样应用嵌套结构

        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);

           }

答案 2 :(得分:0)

在这种情况下,用户是 JSONObject 。我不确定你为什么使用 JSONArray

这应该足够了。

l2 + l3