发布日期:2018-03-26
在Java中如何解析JSON?+ 查看更多
在Java中如何解析JSON?
+ 查看更多
发布日期:2018-03-10 11:05
分类:JAVA
浏览次数:97
我有一个以下的JSON文本要解析,比如获得其中的pageName, pagePic, post_id。代码是怎样的?
{ "pageInfo": { "pageName": "abc", "pagePic": "http://example.com/content.jpg" } "posts": [ { "post_id": "123456789012_123456789012", "actor_id": "1234567890", "picOfPersonWhoPosted": "http://example.com/photo.jpg", "nameOfPersonWhoPosted": "Jane Doe", "message": "Sounds cool. Can't wait to see it!", "likesCount": "2", "comments": [], "timeOfPost": "1234567890" } ] }
回答
org.json库用起来很简单。以下是示例代码:
import org.json.*; JSONObject obj = new JSONObject(" .... "); String pageName = obj.getJSONObject("pageInfo").getString("pageName"); JSONArray arr = obj.getJSONArray("posts"); for (int i = 0; i < arr.length(); i++) { String post_id = arr.getJSONObject(i).getString("post_id"); ...... }
你可以从这获得其他的例子:http://theoryapp.com/parse-json-in-java/
下载Jar包:http://mvnrepository.com/artifact/org.json/json