pageName
次の JSON テキストがあります。これを解析して、、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"
}
]
}
ベストアンサー1
のorg.jsonライブラリは使いやすいです。
覚えておいて欲しいのは(キャストややgetJSONObject
のようなメソッドを使うときgetJSONArray
)、JSON表記では
[ … ]
配列を表すので、ライブラリはそれを解析してJSONArray
{ … }
オブジェクトを表すので、ライブラリはそれを解析してJSONObject
以下のコード例:
import org.json.*;
String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("post_id");
......
}
その他の例は以下からご覧いただけます:JavaでJSONを解析する
ダウンロード可能なjar:http://mvnrepository.com/artifact/org.json/json