티스토리 뷰

반응형


API 로 보통 JSON 파일을 response 할 수 있다.

response 한 JSON 파일을 Parsing 해서 사용하는 법을 정리해보자!


JSON Object 사용을 위해 JSON.simple import 가 필요하다.

plugin으로 사용 시 "여기"를,

jar 파일을 사용할 경우 "여기"를 참고해보면 좋을 것 같다.


API Connection 방법은 아래 글을 참고해보자.

[API] Java API Connection(HttpURLConnection, JSONObject)


JSON Parsing 을 할 때,

JSON 파일에 어떤 구조로 데이터가 저장되어있는지 먼저 잘 파악한 후,

key 에 해당하는 value 를 객체에 알맞게 저장해주면 된다.


.Java JSON Parsing


.Example01


locations 배열에는

위치 번호와 

해당 위치에 귀여운 하리보가 몇 개 있는지의 정보를 가지고 있다.


{

    "locations": [

        {

            "id": 0,

            "located_haribo_count": 0

        },

        {

            "id": 1,

            "located_haribo_count": 4

        },

        {

            "id": 2,

            "located_haribo_count": 4

        },

        {

            "id": 3,

            "located_haribo_count": 4

        }

    ]

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
public ArrayList<Location> getLocations(JSONObject responseJson) {
    ArrayList<Location> locationList = new ArrayList<>();
 
    try {
        // locations 배열이 가지고 있는 value 값을 가져와보자.
        JSONArray locations = responseJson.getJSONArray("locations");
 
        for (int i = 0; i < locations.length(); i++) {
            JSONObject data = locations.getJSONObject(i);
 
            Location location = new Location();
 
            location.setId(data.getInt("id"));
            location.setLocated_haribo_count(data.getInt("located_haribo_count"));
            locationList.add(location);
        }
 
    } catch (JSONException e) {
        e.printStackTrace();
    }
 
    return locationList;
}
cs



.Example02


customers 배열에는

고객 id와

고객이 귀여운 하리보 몇 개를 가지고 있는지,

어느 상점으로 이동할 예정인지의 정보를 가지고 있다.


{

    "customers": [

        {

            "id": 0,

            "located_haribo_count": 0,

            "location_id": 4

        },

        {

            "id": 1,

            "located_haribo_count": 0,

            "location_id": 0

        },

        {

            "id": 2,

            "located_haribo_count": 0,

            "location_id": 0

        },

        {

            "id": 3,

            "located_haribo_count": 0,

            "location_id": 0

        },

        {

            "id": 4,

            "located_haribo_count": 0,

            "location_id": 0

        }

    ]

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
public ArrayList<Customer> getCustomers(JSONObject responseJson) {
    ArrayList<Customer> CustomerList = new ArrayList<>();
 
    try {
        JSONArray Customers = responseJson.getJSONArray("Customers");
        for (int i = 0; i < Customers.length(); i++) {
            JSONObject data = Customers.getJSONObject(i);
 
            Customer customer = new Customer();
 
            customer.setId(data.getInt("id"));
            customer.setLoaded_haribo_count(data.getInt("loaded_haribo_count"));
            customer.setLocation_id(data.getInt("location_id"));
            CustomerList.add(customer);
        }
 
    } catch (JSONException e) {
        e.printStackTrace();
    }
 
    return CustomerList;
}
cs



.Example03


상태(status) 정보와

시간(time) 정보,

거리(distance) 정보를 가지고 있다.


{

    "status": "ready",

    "time": 26,

    "distance": "3.6"

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
public Simulate getSimulate(JSONObject responseJson) {
    Simulate simulate = new Simulate();
 
    try {
        simulate.setStatus(responseJson.getString("status"));
        simulate.setTime(responseJson.getInt("time"));
        simulate.setDistance(responseJson.getFloat("distance"));
 
    } catch (JSONException e) {
        e.printStackTrace();
    }
 
    return simulate;
}
cs



반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday