0

I've got the title, author, and published date from json data to my android app.

I'm trying to get event_start_date and event_location, but failed. can anyone help me how I can get it?

Json:

{
    "status": "ok",
    "count": 1,
    "count_total": 29,
    "pages": 29,
    "posts": [
        {
            "id": 2815,
            "type": "event",
            "slug": "itb-integrated-career-days",
            "url": "http://example.com/event/itb-integrated-career-days/",
            "status": "publish",
            "title": "Title",
            "title_plain": "Title",
            "content": "<p>test</p>\n",
            "excerpt": "<p>test [&hellip;]</p>\n",
            "date": "2015-09-25 01:09:40",
            "modified": "2015-09-29 22:52:35",
            "categories": [],
            "tags": [],
            "author": {
                "id": 1,
                "slug": "john",
                "name": "john",
                "first_name": "",
                "last_name": "",
                "nickname": "john",
                "url": "",
                "description": ""
            },
            "comments": [],
            "attachments": [
                {
                    "id": 2817,
                    "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
                    "slug": "bannertkt102015",
                    "title": "bannertkt102015",
                    "description": "",
                    "caption": "",
                    "parent": 2815,
                    "mime_type": "image/gif",
                    "images": {
                        "full": {
                            "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
                            "width": 1000,
                            "height": 563
                        },
                        "thumbnail": {
                            "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-150x150.gif",
                            "width": 150,
                            "height": 150
                        },
                        "medium": {
                            "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-300x169.gif",
                            "width": 300,
                            "height": 169
                        },
                        "large": {
                            "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
                            "width": 1000,
                            "height": 563
                        },
                        "blog-default": {
                            "url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-806x300.gif",
                            "width": 806,
                            "height": 300
                        }
                    }
                },
                {
                    "id": 2818,
                    "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
                    "slug": "2015-09-25_012008",
                    "title": "2015-09-25_012008",
                    "description": "",
                    "caption": "",
                    "parent": 2815,
                    "mime_type": "image/jpeg",
                    "images": {
                        "full": {
                            "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
                            "width": 589,
                            "height": 529
                        },
                        "thumbnail": {
                            "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-150x150.jpg",
                            "width": 150,
                            "height": 150
                        },
                        "medium": {
                            "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-300x269.jpg",
                            "width": 300,
                            "height": 269
                        },
                        "large": {
                            "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
                            "width": 589,
                            "height": 529
                        },
                        "blog-default": {
                            "url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-589x300.jpg",
                            "width": 589,
                            "height": 300
                        }
                    }
                }
            ],
            "comment_count": 0,
            "comment_status": "open",
            "thumbnail": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-150x150.jpg",
            "custom_fields": {
                "event_location": [
                    "New York"
                ],
                "event_start_date": [
                    "10/23/2015"
                ],
                "event_start_time": [
                    "09:00 AM"
                ],
                "event_start_date_number": [
                    "1445590800"
                ],
                "event_address_country": [
                    "US"
                ],
                "event_address_state": [
                    "Jawdst"
                ],
                "event_address_city": [
                    "New York"
                ],
                "event_address_address": [
                    "Gedung Sasana Budaya Ganesha (Sabuga) Jalan Tamansari 73, New York"
                ],
                "event_address_zip": [
                    "42132"
                ],
                "event_phone": [
                    "5345"
                ],              
                "post_views_count": [
                    "23"
                ]
            }

        }
    ]
}

And my function

public void parseJson(JSONObject json) {
        try {
            info.pages = json.getInt("pages");
            // parsing json object
            if (json.getString("status").equalsIgnoreCase("ok")) {
                JSONArray posts = json.getJSONArray("posts");

                info.feedList = new ArrayList<PostItem>();

                for (int i = 0; i < posts.length(); i++) {
                    Log.v("INFO",
                            "Step 3: item " + (i + 1) + " of " + posts.length());
                    try {
                        JSONObject post = (JSONObject) posts.getJSONObject(i);
                        PostItem item = new PostItem();
                        item.setTitle(Html.fromHtml(post.getString("title"))
                                .toString());
                        item.setDate(post.getString("date"));
                        item.setId(post.getInt("id"));
                        item.setUrl(post.getString("url"));
                        item.setContent(post.getString("content"));
                        if (post.has("author")) {
                            Object author = post.get("author");
                            if (author instanceof JSONArray
                                    && ((JSONArray) author).length() > 0) {
                                author = ((JSONArray) author).getJSONObject(0);
                            }

                            if (author instanceof JSONObject
                                    && ((JSONObject) author).has("name")) {
                                item.setAuthor(((JSONObject) author)
                                        .getString("name"));
                            }
                        }

                        if (post.has("tags") && post.getJSONArray("tags").length() > 0) {
                            item.setTag(((JSONObject) post.getJSONArray("tags").get(0)).getString("slug"));
                        }

                        // TODO do we dear to remove catch clause?
                        try {
                            boolean thumbnailfound = false;

                            if (post.has("thumbnail")) {
                                String thumbnail = post.getString("thumbnail");
                                if (thumbnail != "") {
                                    item.setThumbnailUrl(thumbnail);
                                    thumbnailfound = true;
                                }
                            }

                            if (post.has("attachments")) {

                                JSONArray attachments = post
                                        .getJSONArray("attachments");

                                // checking how many attachments post has and
                                // grabbing the first one
                                if (attachments.length() > 0) {
                                    JSONObject attachment = attachments
                                            .getJSONObject(0);

                                    item.setAttachmentUrl(attachment
                                            .getString("url"));

                                    // if we do not have a thumbnail yet, get
                                    // one now
                                    if (attachment.has("images")
                                            && !thumbnailfound) {

                                        JSONObject thumbnail;
                                        if (attachment.getJSONObject("images")
                                                .has("post-thumbnail")) {
                                            thumbnail = attachment
                                                    .getJSONObject("images")
                                                    .getJSONObject(
                                                            "post-thumbnail");

                                            item.setThumbnailUrl(thumbnail
                                                    .getString("url"));
                                        } else if (attachment.getJSONObject(
                                                "images").has("thumbnail")) {
                                            thumbnail = attachment
                                                    .getJSONObject("images")
                                                    .getJSONObject("thumbnail");

                                            item.setThumbnailUrl(thumbnail
                                                    .getString("url"));
                                        }

                                    }
                                }
                            }

                        } catch (Exception e) {
                            Log.v("INFO",
                                    "Item "
                                            + i
                                            + " of "
                                            + posts.length()
                                            + " will have no thumbnail or image because of exception!");
                            e.printStackTrace();
                        }

                        if (item.getId() != info.ignoreId)
                            info.feedList.add(item);
                    } catch (Exception e) {
                        Log.v("INFO", "Item " + i + " of " + posts.length()
                                + " has been skipped due to exception!");
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
2
  • I recommend you to use GSON library, it will help you to extract data very easily. Commented Oct 5, 2015 at 4:24
  • Custom fields should be the array not object. Commented Oct 5, 2015 at 4:31

1 Answer 1

2

This should do it

JSONObject custom_fields = post.getJSONObject("custom_fields");
JSONArray event_start_date_array = custom_fields.getJSONArray("event_start_date");
JSONArray event_location_array = custom_fields.getJSONArray("event_location");

String event_start_date = event_start_date_array.getString(0);
String event_location = event_location_array.getString(0);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.