Gson 을 이용하여 JsonUtil을 만들어 보았다.

object mapping 과 object to Json

JsonObject 혹은 Json String에서 값 얻어오기 등 지원

https://github.com/jeng832/blog-example/tree/master/JsonUtil

JsonUtil

method list

public static T fromJson(String json, Class clazz)

  • object mapping from Json String to class

public static String toJson(T obj)

  • class to Json String

public static JsonObject parse(String json)

  • Json String to JsonObject

public static String getValue(JsonObject object, String key)

  • get String value of key from JsonObject
  • Support chained key
    • key1.key2.key3
  • Support array index
    • arrKey[10]
    • arrKey[0][2]
  • It can throw NumberFormatException, IndexOutOfBoundsException

public static String getValue(String jsonStr, String key)

  • get String value of key from Json String

public static int getValueAsInteger(String jsonStr, String key)

  • get int value of key from Json String

public static int getValueAsInteger(JsonObject jsonObj, String key)

  • get int value of key from JsonObject

public static long getValueAsLong(String jsonStr, String key)

  • get long value of key from Json String

public static long getValueAsLong(JsonObject jsonObj, String key)

  • get long value of key from JsonObject

public static double getValueAsDouble(JsonObject jsonObj, String key)

  • get double value of key from JsonObject

public static double getValueAsDouble(String jsonStr, String key)

  • get double value of key from Json String

public static float getValueAsFloat(JsonObject jsonObj, String key)

  • get float value of key from JsonObject

public static float getValueAsFloat(String jsonStr, String key)

  • get float value of key from Json String

public static boolean getValueAsBoolean(String jsonStr, String key)

  • get boolean value of key from Json String

public static boolean getValueAsBoolean(JsonObject jsonObj, String key)

  • get boolean value of key from JsonObject

public static boolean contain(String jsonStr, String key)

  • check the key is in the Json String

public static boolean contain(JsonObject jsonObj, String key)

  • check the key is in the JsonObject

+ Recent posts