Translate

2018년 7월 23일 월요일

[Java] String to JSON 라이브러리 정리



Laptop
운영체제Windows 10 Home 64bit
개발프로그램Eclipse Oxygen (4.7)

예제 String:
String jsonString = "{" +
 "'name' : 'Ronaldo', " +
 "'age' : 25, " +
 "'id' : 121.5, " +
 "'lastScores' : [ 2, 1, 3, 5, 0, 0, 1, 1 ]" +
 "}";

1. json Path
https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;

...

System.out.println(document.read("$.name") + " / " + document.read("$.name").getClass().getName());
System.out.println(document.read("$.age") + " / " + document.read("$.age").getClass().getName());
System.out.println(document.read("$.id") + " / " + document.read("$.id").getClass().getName());
System.out.println(document.read("$.lastScores") + " / " + document.read("$.lastScores").getClass().getName());
System.out.println(document.read("$.lastScores[3]") + " / " + document.read("$.lastScores[3]").getClass().getName());

결과:







- 입력된 자료에 따라 반환값 타입이 고정됨. 다른 타입 사용 시 형변환 필요.
- accessors-smart, json-smart, slf4j-api, slf4j-jdk 라이브러리가 함께 필요


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

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

...

JSONParser parser = new JSONParser();
Object obj = null;

// 따옴표 변환 필요
jsonString = jsonString.replace("'", "\"");

try {
 obj = parser.parse(jsonString);
} catch (ParseException e) {
 e.printStackTrace();
}
JSONObject jsonObj = (JSONObject) obj;

System.out.println(jsonObj.get("name") + " / " + jsonObj.get("name").getClass().getName());
System.out.println(jsonObj.get("age") + " / " + jsonObj.get("age").getClass().getName());
System.out.println(jsonObj.get("id") + " / " + jsonObj.get("id").getClass().getName());
System.out.println(jsonObj.get("lastScores") + " / " + jsonObj.get("lastScores").getClass().getName());

JSONArray jsonArray = (JSONArray) jsonObj.get("lastScores");
System.out.println(jsonArray.get(3) + " / " + jsonArray.get(3).getClass());

결과:


입력된 자료에 따라 반환값 타입이 고정됨. 다른 타입 사용 시 형변환 필요.
- JSONArray 사용 시 추가적인 JSONArray 선언이 필요


3. Gson

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

...

JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonString);

System.out.println(element.getAsJsonObject().get("name").getAsString());
System.out.println(element.getAsJsonObject().get("age").getAsFloat());
System.out.println(element.getAsJsonObject().get("lastScores").getAsJsonArray().get(3));

결과:




- .getAs... 메소드로 형변환 가능 
- JSONArray의 경우 getAsJsonArray 사용

4. boon

POJO 형식의 클래스 필요.

Player.class

package jsonTest;

import java.util.ArrayList;

public class Player {
 String name;
 int age;
 float id;
 ArrayList lastScores;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public float getId() {
  return id;
 }
 public void setId(float id) {
  this.id = id;
 }
 public ArrayList getLastScores() {
  return lastScores;
 }
 public void setLastScores(ArrayList lastScores) {
  this.lastScores = lastScores;
 }
}

(ArrayList 대신 int[]형으로 써도 무방함)

import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

import jsonTest.Player;

...

ObjectMapper mapper = new JsonFactory().create();
Player p = mapper.fromJson(jsonString, Player.class);

System.out.println(p.getName());
System.out.println(p.getLastScores().get(3));

결과:



- POJO로 형변환 문제 해결 가능
- Java의 데이터형 객체를 활용 가능

참고 사이트:
http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html

0 개의 댓글:

댓글 쓰기