0

I think, I have the understanding of JSON down, but I am having a slight issue with reading inner objects, for example Cover in this situation.

    {
       "id": "19292868552",
       "about": "Build, grow, and monetize your app with Facebook.\nhttps://developers.facebook.com/",
       "can_post": false,
       "category": "Product/service",
       "checkins": 1,
       "company_overview": "Visit https://developers.facebook.com for more information on how to build, grow, and monetize your app.\n\nIf you have questions about using Facebook or need help with general inquiries, visit https://www.facebook.com/facebook or our Help Center at http://www.facebook.com/help.\n\nIf you need to report bugs, appeal apps, or ask detailed technical questions, visit the following:\nAppeal Apps: https://developers.facebook.com/appeal\nReport Bugs: http://developers.facebook.com/bugs\nTechnical Questions: http://facebook.stackoverflow.com/",
       "cover": {
          "cover_id": "10152004458663553",
          "offset_x": 0,
          "offset_y": 0,
          "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/s720x720/1466030_10152004458663553_1984809612_n.jpg?oh=97b895edc21d21c0f40a67a6de6077bd&oe=54BBD66C&__gda__=1422540117_30f73303c987294f8ffccd193d190941"
       },
       "has_added_app": false,
       "is_community_page": false,
       "is_published": true,
       "likes": 3262128,
       "link": "https://www.facebook.com/FacebookDevelopers",
       "name": "Facebook Developers",
       "parking": {
          "lot": 0,
          "street": 0,
          "valet": 0
       },
       "talking_about_count": 10066,
       "username": "FacebookDevelopers",
       "website": "http://developers.facebook.com",
       "were_here_count": 0
    }

I was hoping to have this read from the JSON, it all works until I hit the Cover part. I have not tried to access the parking object yet, but I assume the issue will be the same.

    public FacebookCover createFacebookCoverObject (String json) throws 
        JSONException {

    FacebookCover facebookCover = new FacebookCover();


    JSONObject obj = new JSONObject(json);



    facebookCover.setAbout(obj.getString("about"));
    facebookCover.setCategory(obj.getString("category"));
    facebookCover.setCompanyOverview(obj.getString("company_overview"));
    facebookCover.setId(obj.getString("id"));
    facebookCover.setIsPublished(obj.getBoolean("is_published"));
    facebookCover.setLikes(obj.getInt("likes"));
    facebookCover.setLink(obj.getString("link"));
    facebookCover.setName(obj.getString("name"));
    facebookCover.setTalkingAboutCount(obj.getInt("talking_about_count"));
    facebookCover.setUserName(obj.getString("username"));
    facebookCover.setWebsite(obj.getString("website"));
    facebookCover.setWereHereCount(obj.getInt("were_here_count"));

    Cover theCover = new Cover();

    JSONObject obj2 = new JSONObject();

    theCover.setCoverId((obj2.getString("cover_id")));
    theCover.setOffSetX(obj2.getInt("offset_x"));
    theCover.setOffSetY(obj2.getInt("offset_y"));
    theCover.setSource(obj2.getString("source"));

    return facebookCover;

}

Here is also my test method.

public void testCreateFavebookCoverObject() throws Exception {
    System.out.println("createFavebookCoverObject");
    String url = "https://graph.facebook.com/19292868552/";
    InputStream is = new URL(url).openStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String s = null;

    s = rd.readLine();
    System.out.println(s);
    FacebookCover instance = new FacebookCover();
    FacebookCover result = instance.createFacebookCoverObject(s);
}
2
  • 2
    Why would JSONObject obj2 = new JSONObject(); contain anything? Commented Oct 7, 2014 at 15:27
  • Sorry I meant to change it...that was my issue. Was being fairly dumb. Commented Oct 8, 2014 at 0:50

1 Answer 1

5

It should do the trick :

JSONObject obj2 = obj.getJSONObject("cover");
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that should do it.
Thanks guys, the solved it...I should have know that. However now when I pass the JSON string, I am getting the error message must begin with '{' at character 1. I am starting to heavily dislike JSON. I added my testFacebookCover to the block above.

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.