본문 바로가기

시행착오/[java]

[java] 자바에서 String을 json 객체로 변환 - json-simple 라이브러리

zzznara2.tistory.com/673

 

[java] 자바에서 String을 json 객체로 변환하는 방법

java에서 String으로 돼 있는 json을 JSONObject로 변환해서 json을 사용하는 샘플예제입니다. 저도 이거 찾느라 한참을 뒤졌네요. import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser..

zzznara2.tistory.com

tychejin.tistory.com/139

 

[JAVA] JSON library json-simple 사용법

json-simple 이란? JSON 데이터를 처리하기 위한 자바 라이브러리입니다. json-simple 특징 1.json-simple은 내부적으로 JSON 데이터를 처리하기 위해 Map과 List를 사용합니다. 2.json-simple은 JSON 데이터를 구..

tychejin.tistory.com

Maven 설정

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

 

String => json example code

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

String jsonStr = "{"
			+ "		code:'1000',"
			+ "		name:'포도'"
			+ "}"
			;

JSONParser parser = new JSONParser();
Object obj = parser.parse( jsonStr );
JSONObject jsonObj = (JSONObject) obj;

String code = (String) jsonObj.get("code");
String name = (String) jsonObj.get("name");