본문 바로가기
개발/Java

[Java] google json-simple 사용하기

by 코딩하는 흰둥이 2023. 5. 8.
dependency
// gradle
<!-- google Json -->
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

<!-- google gson -->
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'



// maven
<!-- google Json -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
	<artifactId>json-simple</artifactId>
	<version>1.1.1</version>
</dependency>

<!-- google gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

 

 

 

JSONObject
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class RunTestController {
    public static void main(String[] args) throws Exception {

        /**
         * Json 넣기
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", "흰둥이");
        jsonObject.put("password", "1234");
        jsonObject.put("addr", "대한민국");
        jsonObject.put("age", "1");

        System.err.println(jsonObject);
	}
}

 

 

Pretty  Print 예쁘게 출력하기
        /**
         * pretty print 예쁘게 출력하기
         */
        String jsonText =  "{\"id\":\"흰둥이\",\"password\":\"1234\",\"addr\":\"대한민국\",\"age\":1}";

	// Gson 을 이용하여 출력한다
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(jsonText);
        String prettyJsonString = gson.toJson(je);

        System.err.println(prettyJsonString);

 

 

 

Parsing 파싱하기1
        /**
         * google json-simple parsing 하기
         */

        String jsonText =  "{\"id\":\"흰둥이\",\"password\":\"1234\",\"addr\":\"대한민국\",\"age\":1}";
        
        // JsonParser 와 JSONParser 는 다르다
        JSONParser parser = new JSONParser();
        JSONObject jo = null;
        jo = (JSONObject) parser.parse(jsonText);

        System.err.println(jo.get("id"));
        System.err.println(jo.get("addr"));

 

 

Parsing 파싱하기2
String jsonText2 =
                "{"
                + "\"map1\": {"
                +       "\"id\": \"흰둥이\","
                +       "\"password\": \"1234\","
                +       "\"addr\": \"대한민국\","
                +       "\"age\": \"1\"},"
                + "\"map2\": {"
                +       "\"id\": \"검둥이\","
                +       "\"password\": \"5678\","
                +       "\"addr\": \"제주도\","
                +       "\"age\": \"2\" }"
                +"}";


        JSONParser parser2 = new JSONParser();
        JSONObject jo2 = (JSONObject) parser2.parse(jsonText2);

        System.err.println("map1 : "+jo2.get("map1"));
        System.err.println("map2 : "+jo2.get("map2"));

        // json 안의 map1 에서 id 만 꺼내기
        JSONObject jo3 = (JSONObject) jo2.get("map1");
        System.err.println("map1 id : "+jo3.get("id"));

댓글