ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] JSON library json-simple 사용법
    Java/기타 2019. 6. 28. 15:08
    반응형

    json-simple 이란?

    JSON 데이터를 처리하기 위한 자바 라이브러리입니다.

     

    json-simple 특징

    1.json-simple은 내부적으로 JSON 데이터를 처리하기 위해 Map과 List를 사용합니다.

    2.json-simple은 JSON 데이터를 구문 분석하고 JSON을 파일에 기록할 수 있습니다.

    3.json-simple의 가장 큰 특징은 타사 라이브러리에 대한 의존성이 없습니다.

    4.json-simple는 매우 가벼운 API이며 간단한 JSON 데이터를 처리하기 위해 적합합니다.

      

    json-simple 주요 클래스

    org.json.simple Class JSONObejct

     - JSON 객체를 추상화한 클래스로, java.util.HashMap 클래스를 상속받고 있으므로 대부분의 메소드가 HashMap 클래스로부터 상속받고 있습니다.

    org.json.simple Class JSONArray

     - JSON 배열을 추상화한 클래스로, java.util.ArrayList 클래스를 상속하고 있으므로 메소드 사용 방법은 대부분 ArrayList와 거의 흡사합니다.

    org.json.simple Class JSONParser

     - JSON 데이터를 파싱하는 기능을 구현한 클래스입니다.

    org.json.simple Class JSONValue

     - JSON 데이터를 다루기 위한 몇 가지 메소드드을 제공합니다.

    org.json.simple Class JSONException

     - JSONParser 클래스를 사용해서 파싱할 때 발생할 수 있는 예외 사항을 추상화한 클래스입니다. 

      

    Maven 설정

    1
    2
    3
    4
    5
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    cs

     

    MVNrepository : https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple

     

    1.파일에 JSON을 작성

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    package com.tychejin.study.json;
     
    import java.io.FileWriter;
    import java.io.IOException;
     
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
     
    public class JsonSimpleExample01 {
     
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
     
            JSONObject obj = new JSONObject();
            obj.put("name""tychejin.tistory.com");
            obj.put("age"2019);
     
            JSONArray list = new JSONArray();
            list.add("messages01");
            list.add("messages02");
            list.add("messages03");
     
            obj.put("messages", list);
     
            try (FileWriter file = new FileWriter("c:\\Data.json")) {
                file.write(obj.toJSONString());
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            System.out.print(obj);
        }
    }
     
    cs

     

    2.파일에서 JSON을 읽기 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package com.tychejin.study.json;
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Iterator;
     
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
     
    public class JsonSimpleExample02 {
        
        public static void main(String[] args) {
     
            JSONParser parser = new JSONParser();
     
            try (Reader reader = new FileReader("c:\\Data.json")) {
     
                JSONObject jsonObject = (JSONObject) parser.parse(reader);
                System.out.println(jsonObject);
     
                String name = (String) jsonObject.get("name");
                System.out.println(name);
     
                long age = (Long) jsonObject.get("age");
                System.out.println(age);
     
                // loop array
                JSONArray msg = (JSONArray) jsonObject.get("messages");
                Iterator<String> iterator = msg.iterator();
                while (iterator.hasNext()) {
                    System.out.println(iterator.next());
                }
     
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
     
    cs

     

    JSON 변환 (String, Map, List, JSONString, JSONObject, JSONArray) - https://tychejin.tistory.com/311

     

     

    반응형

    댓글

Designed by Tistory.