如何在Java中解析JSON [英] How to parse JSON in Java
本文介绍了如何在Java中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON文本。如何解析它以获得 pageName
, pagePic
, post_id
等等?
I have the following JSON text. How can I parse it to get pageName
, pagePic
, post_id
, etc.?
{
"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 库易于使用。示例代码如下:
The org.json library is easy to use. Example code below:
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");
......
}
您可以从以下网站找到更多示例:用Java解析JSON
You may find more examples from: Parse JSON in Java
可下载的jar: http://mvnrepository.com/artifact/org.json/json
这篇关于如何在Java中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文