dependency에 추가
implementation 'org.json:json:20190722'
String jsonData = "{\n" +
" \"items\": [\n" +
" {\n" +
" \"label\": \"블루 진\",\n" +
" \"price\": 80000\n" +
" },\n" +
" {\n" +
" \"label\": \"후드 티\",\n" +
" \"price\": 30000\n" +
" },\n" +
" {\n" +
" \"label\": \"무지 반팔\",\n" +
" \"price\": 200000\n" +
" }\n" +
" ]\n" +
"}";
배열 형식으로 제공 될 때
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("items");
List<JSONObject> items = new ArrayList<>();
for(int i=0; i<jsonArray.length(); i++){
items.add(jsonArray.getJSONObject(i));
}
Collections.sort(items, new Comparator<JSONObject>(){
@Override
public int compare(JSONObject a, JSONObject b){
int aPrice = a.getInt("price");
int bPrice = b.getInt("price");
return bPrice - aPrice;
}
});
{"price":80000,"label":"블루 진"}
{"price":30000,"label":"후드 티"}
{"price":20000,"label":"무지 반팔"}