得到JSON数组的Andr​​oid的Java的JSON数组数据 [英] get data from Json array in Json array Android Java

查看:134
本文介绍了得到JSON数组的Andr​​oid的Java的JSON数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好如何让在另一个JSON数组我有获取数据到附件,但附件不工作的JSON数组数据,所有code的工作,直到附件如何从附件的数据,我需要从它那里得到photo_75

Hello how to get data from Json Array in another Json array i have get data till attachments but attachment doesnt work, All code work till attachments how to get data from attachments I need to get "photo_75" from it

的Json

"response":{  
  "count":3,
  "items":[  
     {  
        "id":3,
        "from_id":205110032,
        "owner_id":-81865402,
        "date":1417672154,
        "post_type":"post",
        "text":"jjjjASDFGHJKYTRDXCVB",
        "attachments":[  
           {  
              "type":"photo",
              "photo":{  
                 "id":330414711,
                 "album_id":-7,
                 "owner_id":205110032,
                 "photo_75":"http:\/\/cs605116.vk.me\/v605116032\/6325\/3SwTo8j4lJ0.jpg",
                 "photo_130":"http:\/\/cs605116.vk.me\/v605116032\/6326\/_OZA86FO3Nw.jpg",
                 "photo_604":"http:\/\/cs605116.vk.me\/v605116032\/6327\/AUtB59708Nw.jpg",
                 "photo_807":"http:\/\/cs605116.vk.me\/v605116032\/6328\/59oAdfz9jgI.jpg",
                 "width":538,
                 "height":807,
                 "text":"",
                 "date":1399134687,
                 "access_key":"7297eb663de2e4e6b2"
              }
           }
        ],
        "comments":{  
           "count":0
        },
        "likes":{  
           "count":0
        },
        "reposts":{  
           "count":0
        }
     },

的Java

private void parseJsonFeed(JSONObject response) {
    try {
        JSONObject parent =  response.getJSONObject("response");

        JSONArray feedArray = parent.getJSONArray("items");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);

            FeedItem item = new FeedItem();
            item.setId(feedObj.getInt("id"));           


            item.setName(feedObj.getString("post_type"));
            item.setTimeStamp(feedObj.getString("date"));


            // Image might be null sometimes
            String image = feedObj.isNull("photo") ? null : feedObj
                    .getString("photo");
            item.setImge(image);
            item.setStatus(feedObj.getString("text"));

        All code work till there how to get data from attachments
             ***JSONObject response1 = response.getJSONObject("response");
             feedArray = parent.getJSONArray("items");***

            JSONArray feedArray1 = response1.getJSONArray("attachments");

            for (int i1 = 0; i1 < feedArray1.length(); i1++) {
                 JSONObject  feedObj1 = (JSONObject) feedArray1.get(i1);

                 FeedItem item1 = new FeedItem();

                item.setProfilePic(feedObj1.getString("photo_75"));


             }



            // url might be null sometimes
            String feedUrl = feedObj.isNull("url") ? null : feedObj
                    .getString("url");
            item.setUrl(feedUrl);

            feedItems.add(item);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

在此先感谢

推荐答案

您正在寻找在错误的对象的附件。 attachmetnts是项属性。 而不是

you are looking for attachments in wrong object. "attachmetnts" is property of item. instead of

JSONArray feedArray1 = response1.getJSONArray("attachments");

使用

JSONArray feedArray1 = feedObj.getJSONArray("attachments");

在你的情况的 feedObj包含项目对象。

,以达到照片: 删除行:

to get photo : Remove lines :

        String image = feedObj.isNull("photo") ? null : feedObj
                .getString("photo");
        item.setImge(image);

和它更改为:

    for (int i1 = 0; i1 < feedArray1.length(); i1++) {
            JSONObject  attachment = (JSONObject) feedArray1.get(i1);
            JSONObject photo = (JSONObject) attachment.getJSONObject("photo");
            item.setImge(photo);
            item.setProfilePic(photo.getString("photo_75"));
            item.setStatus(photo.getString("text"));
         }

这篇关于得到JSON数组的Andr​​oid的Java的JSON数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆