자바에서 JSON 데이터를 어떻게 파싱하나요?
조회수 24519회
다음의 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"
}
]
}
1 답변
-
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"); ...... }
더 많은 예제가 궁금하시다면 해당 사이트를 참고하세요: Parse JSON in Java
jar 파일 다운로드: http://mvnrepository.com/artifact/org.json/json
댓글 입력