관리 메뉴

ふたりで

response Body에 담긴 Json 문자 파싱 본문

Spring

response Body에 담긴 Json 문자 파싱

graykang 2020. 12. 1. 09:41
728x90
반응형
SMALL

- dependency  

		<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1.1</version>
		</dependency>

 

 

- 구현 Class

	public Map<String, Object> getAPICall(int param) throws Exception {
    		Map <String, Object> resultMap = new HashMap<String,Object>();
		
    		String reqStr = uRLUtil.requestGET("http://aaa.aaa.kr/GETjson", param);
            /*
             reqStr에 담겨오는 jsonString
               {	"code": "success",
                	"message": "",	
                	"data": {
            		"a_unit": "2000.9",
            		"b_unit": "3000.0"	
               	     }
                }
             */

            //jsonString 파싱하여 jsonObject에 담는다.
            JSONParser parser = new JSONParser();
    		Object objP = parser.parse( reqStr );
    		JSONObject jsonObjP = (JSONObject) objP;//root
    		Object objC =jsonObjP.get("data");
    		JSONObject jsonObjC = (JSONObject) objC;//child
		
            //jsonObject에 담긴걸 Map에 담아 리턴.
    		resultMap.put("code", jsonObjP.get("code"));
    		resultMap.put("message", jsonObjP.get("message"));
    		resultMap.put("sms_unit", jsonObjC.get("a_unit"));
    		resultMap.put("lms_unit", jsonObjC.get("b_unit"));
    		return resultMap;
	}
728x90
반응형
LIST
Comments