简体   繁体   中英

Trouble in Parsing multi Json to Java/Android

Hello I am newbie in Android I want to parse Json but this one not simply Json how to solve them and I have problem with parsing this Json is there auto tools to parse them I need to get id name photo text pic_photo url

response: {
    count: 3,
    items: [{
    id: 3,
    from_id: 205110032,
    owner_id: -81865402,
    date: 1417672154, 
    post_type: 'post',
    text: 'jjjjASDFGHJKYTRDXCVB',
     can_edit: 1, 
      created_by: 205110032,
      can_delete: 1,
      attachments: [{
      type: 'photo',
       photo: {
       id: 330414711,
        album_id: -7,
        owner_id: 205110032,
       photo_75: 'https://pp.vk.me/...5/3SwTo8j4lJ0.jpg',
       photo_130: 'https://pp.vk.me/...6/_OZA86FO3Nw.jpg',
         photo_604: 'https://pp.vk.me/...7/AUtB59708Nw.jpg',
        photo_807: 'https://pp.vk.me/...8/59oAdfz9jgI.jpg',
        width: 538,
         height: 807,
       text: '',
        date: 1399134687,
       access_key: 'a54d74c6fce6694852'
        }
        }],
        post_source: {
         type: 'vk'
        },
         comments: {
        count: 0,
        can_post: 1
        },
         likes: {
        count: 0,
       user_likes: 0,
       can_like: 1,
      can_publish: 0
       },
      reposts: {
       count: 0,
         user_reposted: 0
        }
       }, {
        id: 2,
       from_id: 205110032,
        owner_id: -81865402,
         date: 1417621480,
        post_type: 'post',
          text: 'This is workk!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1',
          can_edit: 1,
         created_by: 205110032,
        can_delete: 1,
          attachments: [{
         type: 'photo',
          photo: {
          id: 330414711,
            album_id: -7,
            owner_id: 205110032,
           photo_75: 'https://pp.vk.me/...5/3SwTo8j4lJ0.jpg',
           photo_130: 'https://pp.vk.me/...6/_OZA86FO3Nw.jpg',
           photo_604: 'https://pp.vk.me/...7/AUtB59708Nw.jpg',
            photo_807: 'https://pp.vk.me/...8/59oAdfz9jgI.jpg',
           width: 538,
           height: 807,
          text: '',
          date: 1399134687,
          access_key: 'a54d74c6fce6694852'
          }
          }],
          post_source: {
           type: 'vk'
           },
           comments: {
           count: 0,
          can_post: 1
             },
          likes: {
           count: 0,
           user_likes: 0,
         can_like: 1,
           can_publish: 0
         },
          reposts: {
            count: 0,
           user_reposted: 0
          }
         }, {
         id: 1,
           from_id: 205110032,
         owner_id: -81865402,
           date: 1417620518,
           post_type: 'post',
           text: 'aaaaa',
          can_edit: 1,
          created_by: 205110032,
          can_delete: 1,
           attachments: [{
        type: 'photo',
        photo: {
        id: 330414774,
         album_id: -6,
          owner_id: 205110032,
           photo_75: 'https://pp.vk.me/...5/3SwTo8j4lJ0.jpg',
            photo_130: 'https://pp.vk.me/...6/_OZA86FO3Nw.jpg',
              photo_604: 'https://pp.vk.me/...7/AUtB59708Nw.jpg',
           photo_807: 'https://pp.vk.me/...8/59oAdfz9jgI.jpg',
           width: 538,
        height: 807,
        text: '',
           date: 1399134750,
       post_id: 135
       }
       }],
         post_source: { type: 'vk' }, comments: { count: 0, can_post: 1 }, likes: { count: 0, user_likes: 0, can_like: 1, can_publish: 0 }, reposts: { count: 0, user_reposted: 0 } }] }

Please help even show an tutorial for android I have tried but not worked

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("name"));

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

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

            feedItems.add(item);
        }

        // notify data changes to list adapater
        listAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

I created a GitHub project that consumes your service at https://api.vk.com/method/wall.get?owner_id=-81865402&v=5.27 and converts date.

Sample code:

@Override
public void response(String response, Object... parameters) {
    stateTextView.setText("Getting Data Completed");

    ArrayList<Item> items = new JsonParser(response).getItems();
    Item item = items.get(1);
    PhotoAttachment photoAttachment = (PhotoAttachment)item.getAttachments().get(0);

    DateFormat formatter = new SimpleDateFormat("ss");
    Date date = null;
    try {
        date = formatter.parse(photoAttachment.getDate());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    stateTextView.setText(item.getText() + "\n\n"
                            + photoAttachment.getPhoto_75() + "\n\n"
                            + date.toString() + "\n\n"
                            + date.getDate() + "-" + (date.getMonth()+1) + "-" + (date.getYear()+1900) + " " 
                            + date.getHours()  + ":" + date.getMinutes() + ":" + date.getSeconds() + "\n\n");

    // you can move date conversion to the class date setter.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM