자바에서 JSON 코드를 파싱하고 싶은데 어떻게해요?
조회수 3160회
발생하는 문제 및 실행환경
밑의 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"); ...... }
다른 예제가 필요하시면 링크들어가세요.
댓글 입력