개발일기 정답찾기

[JAVA] JSON Array Parsing 제이슨 배열 파싱 본문

IT/java

[JAVA] JSON Array Parsing 제이슨 배열 파싱

유딩동 2021. 6. 16. 00:02

안녕하세요 유딩동입니다.

JSON Array 파싱 하는 방법 입니다.

- JSON 이란?

- 표기 방법

- JSON Array Parsing 예제

JSON 이란?

= JavaScript Object Notation

속성-값 쌍( attribute–value pairs and array data types (or any other serializable value)) 또는 "키-값 쌍"으로 이루어진 데이터 오브젝트를 전달하기 위해 인간이 읽을 수 있는 텍스트를 사용하는 개방형 표준 포맷

 

본래는 자바스크립트 언어로부터 파생되어 자바스크립트의 구문 형식을 따르지만 언어 독립형 데이터 포맷이다. 즉, 프로그래밍 언어나 플랫폼에 독립적이므로, 구문 분석 및 JSON 데이터 생성을 위한 코드는 C, C++, C#, 자바, 자바스크립트, 펄, 파이썬 등 수많은 프로그래밍 언어에서 쉽게 이용할 수 있다.

 

표기방법

 

항상 큰 따옴표(")로 묶어야 하며, 그 안에는 유니코드 문자들이 나열된다. 유니코드 중 역슬래시(\)와 큰따옴표(")는 바로 사용할 수 없다. 역슬래시는 제어문자를 표현하기 위해 사용되며 다음과 같은 의미를 지닌다.

 

 

JSON Array Parsing 예제

String으로 먼저, json 형식의 데이터를 두개 선언 해 줍니다.

list 안에 있는 데이터를 가져오기 위한 예제는 다음과 같습니다.

 

import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class udingtest {

	
	public static void main(String args[]) {
        String jsonInfo = "{\"code\":\"success\",\"data\":{\"list\":[{\"key\":\"udingudingdongdong\",\"id\":\"11uding\",\"name\":\"유딩동\",\"sex\":\"여성\",\"regdate\":\"2020-11-30 13:23:56\",\"updatedate\":\"2021-03-25 17:28:08\",\"use\":true},"
        		+ "{\"key\":\"uuding2\",\"id\":\"uding\",\"name\":\"유딩동생\",\"sex\":\"남성\",\"regdate\":\"2021-05-01 10:00:00\",\"updatedate\":\"2021-06-08 14:28:08\",\"use\":false}"
        		+ "]}}";
		
		HashMap map = new HashMap();
		
        JSONObject jsonObject = JSONObject.fromObject(jsonInfo);
        System.out.println("=====jsonObject=====");
        System.out.println(jsonObject.toString());

        if ("success".equals(jsonObject.get("code"))) {
        	JSONObject tempJson = (JSONObject) jsonObject.get("data");
            JSONArray respArr = (JSONArray) tempJson.get("list");

            System.out.println("=====list parsing=====");
            for (int i = 0; i < respArr.size(); i++) {
            	
                JSONObject resObject = (JSONObject) respArr.get(i);
                
                map.put("profile_key",     resObject.get("key"));
                map.put("profile_id",      resObject.get("id"));
                map.put("profile_nm",      resObject.get("name"));
                map.put("sex",             resObject.get("sex"));

                map.put("reg_dt",          resObject.get("regdate"));
                map.put("update_dt",       resObject.get("updatedate"));
                map.put("use",             resObject.get("use"));
                System.out.println(map.toString());
            }
        
        }
        
	}
}
=====jsonObject=====
{"code":"success","data":{"list":[{"key":"udingudingdongdong","id":"11uding","name":"유딩동","sex":"여성","regdate":"2020-11-30 13:23:56","updatedate":"2021-03-25 17:28:08","use":true},{"key":"uuding2","id":"uding","name":"유딩동생","sex":"남성","regdate":"2021-05-01 10:00:00","updatedate":"2021-06-08 14:28:08","use":false}]}}
=====list parsing=====
{update_dt=2021-03-25 17:28:08, sex=여성, profile_id=11uding, profile_key=udingudingdongdong, use=true, reg_dt=2020-11-30 13:23:56, profile_nm=유딩동}
{update_dt=2021-06-08 14:28:08, sex=남성, profile_id=uding, profile_key=uuding2, use=false, reg_dt=2021-05-01 10:00:00, profile_nm=유딩동생}

 

JSON 관련하여 더 많은 정보로 찾아올게요!

Comments