2018년 3월 20일 화요일

[Java] JSON Parsing Examples


1. Parse JSON data with org.json.simple library

Download (json-simple-1.1.1.jar)
Download from Maven

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JavaJsonSimple
{
    public static void main(String[] args) {

        String jsonStr = "{\"members\":["
                + "{\"name\":\"Tom\","
                + "\"email\":\"tom@email.com\","
                + "\"age\":\"30\""
                + "},"
                + "{\"name\":\"Jane\","
                + "\"email\":\"jane@email.com\","
                + "\"age\":\"28\""
                + "}]}";

        try {
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonStr);
            JSONArray memberArray = (JSONArray) jsonObj.get("members");

            System.out.println("===== Members (org.json.simple) =====");
            for(int i=0 ; i<memberArray.size() ; i++){
                JSONObject tempObj = (JSONObject) memberArray.get(i);
                System.out.println("["+(i+1)+"] Name: "+tempObj.get("name"));
                System.out.println("["+(i+1)+"] Email: "+tempObj.get("email"));
                System.out.println("["+(i+1)+"] Age: "+tempObj.get("age"));
                System.out.println("----------------------------");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


Result:
===== Members (org.json.simple) =====
[1] Name: Tom
[1] Email: tom@email.com
[1] Age: 30
----------------------------
[2] Name: Jane
[2] Email: jane@email.com
[2] Age: 28
----------------------------


2. Parse JSON data with org.json library

Download (org.json-20180130.jar)
Download from Maven

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JavaJson {

   public static void main(String[] args) throws JSONException {
       
        String jsonStr = "{\"members\":["
            + "{\"name\":\"Tom\","
            + "\"email\":\"tom@email.com\","
            + "\"age\":\"30\""
            + "},"
            + "{\"name\":\"Jane\","
            + "\"email\":\"jane@email.com\","
            + "\"age\":\"28\""
            + "}]}";
           
        JSONObject jObject = new JSONObject(jsonStr);
        JSONArray membersArray = jObject.getJSONArray("members");

        System.out.println("===== Members (org.json) =====");
        for(int i=0 ; i< membersArray.length() ; i++) {
            JSONObject memberObject = membersArray.getJSONObject(i);
            System.out.println("["+(i+1)+"] Name: "+ memberObject.getString("name"));
            System.out.println("["+(i+1)+"] Email: "+ memberObject.getString("email"));
            System.out.println("["+(i+1)+"] Age: "+ memberObject.getString("age"));       
            System.out.println("----------------------------");
        }    
   }
}

Result:
===== Members (org.json) =====
[1] Name: Tom
[1] Email: tom@email.com
[1] Age: 30
----------------------------
[2] Name: Jane
[2] Email: jane@email.com
[2] Age: 28
----------------------------









댓글 없음:

댓글 쓰기