我认为,我对JSON有所了解,但我在阅读内部对象方面遇到了一些问题,例如在这种情况下Cover
。
{
"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
}
我希望能从JSON中读取这些内容,直到我点击Cover部分才一直有效。我还没有尝试访问parking
对象,但我认为问题是一样的。
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;
}
这也是我的测试方法。
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);
}
答案 0 :(得分:5)
应该这样做:
JSONObject obj2 = obj.getJSONObject("cover");