1import org.json.*;
2
3String jsonString = ... ; //assign your JSON String here
4JSONObject obj = new JSONObject(jsonString);
5String pageName = obj.getJSONObject("pageInfo").getString("pageName");
6
7JSONArray arr = obj.getJSONArray("posts");
8for (int i = 0; i < arr.length(); i++)
9{
10 String post_id = arr.getJSONObject(i).getString("post_id");
11 ......
12}
1//from object to JSON
2Gson gson = new Gson();
3gson.toJson(yourObject);
4
5// from JSON to object
6yourObject o = gson.fromJson(JSONString,yourObject.class);
1public static void main(String[] args) throws Exception {
2 JSONObject jsonObject = (JSONObject) readJsonSimpleDemo("example.json");
3 System.out.println(jsonObject);
4 System.out.println(jsonObject.get("age"));
5}
6
7public static Object readJsonSimpleDemo(String filename) throws Exception {
8 FileReader reader = new FileReader(filename);
9 JSONParser jsonParser = new JSONParser();
10 return jsonParser.parse(reader);
11}
12
1JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");
2
3List<String> list = new ArrayList<String>();
4JSONArray array = obj.getJSONArray("interests");
5for(int i = 0 ; i < array.length() ; i++){
6 list.add(array.getJSONObject(i).getString("interestKey"));
7}