よく使っているJacksonを使ったJSON⇔Object相互変換クラスです。
pomのdependency設定
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
JsonConverterクラス
JsonConverter.java
import java.io.IOException;
import java.security.InvalidParameterException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonConverter {
private static ObjectMapper mapper = new ObjectMapper();
private JsonConverter() {
// do nothing.
}
public static String toString(final Object object) throws JsonGenerationException, JsonMappingException,
IOException {
String json = mapper.writeValueAsString(object);
return json;
}
public static <T> T toObject(final String jsonString, final Class<T> clazz) throws JsonParseException,
JsonMappingException, IOException {
T object = null;
if (jsonString == null) {
throw new InvalidParameterException("jsonString is null.");
}
object = mapper.readValue(jsonString, clazz);
return object;
}
public static <T> T toObject(String jsonString,
TypeReference<T> valueTypeRef) throws JsonParseException, JsonMappingException, IOException {
T object = null;
if (jsonString == null) {
throw new InvalidParameterException("jsonString is null.");
}
object = mapper.readValue(jsonString, valueTypeRef);
return object;
}
}
テストに使うJSON文字列
{
"value": 100,
"string": "hogehoge",
"map": {
"key2": "value2",
"key1": "value1"
},
"list": [
"item1",
"item2"
]
}
これを"SAMPLE_JSON"として定義します。
private static final String SAMPLE_JSON = "{\"value\":100,\"string\":\"hogehoge\",\"map\":{\"key2\":\"value2\",\"key1\":\"value1\"},\"list\":[\"item1\",\"item2\"]}";
テストに使うJSONObjectクラス
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonObject {
public int value;
public String string;
public Map<String, String> map;
public List<String> list;
}
JSON⇔Objectの相互変換
- コード
// JSON文字列⇒Object
JsonObject jsonObject = JsonConverter.toObject(SAMPLE_JSON, JsonObject.class);
// Object⇒JSON文字列
String jsonString = JsonConverter.toString(jsonObject);
// JSON文字列出力
System.out.println(jsonString);
- 出力結果(見やすいように整形済み)
{
"value": 100,
"string": "hogehoge",
"map": {
"key2": "value2",
"key1": "value1"
},
"list": [
"item1",
"item2"
]
}
JSON⇔Listの相互変換
- コード
JsonObject jsonObject = JsonConverter.toObject(SAMPLE_JSON, JsonObject.class);
// List⇒JSON文字列
List<JsonObject> jsonObjects = new ArrayList<JsonObject>();
jsonObjects.add(jsonObject);
String jsonString = JsonConverter.toString(jsonObjects);
System.out.println("jsonString=" + jsonString);
// JSON文字列⇒List
jsonObjects = JsonConverter.toObject(jsonString, new TypeReference<List<JsonObject>>() {
});
for (JsonObject obj : jsonObjects) {
System.out.println(obj.value);
System.out.println(obj.string);
for(String str : obj.list) {
System.out.println(str);
}
for(Map.Entry<String, String> e : obj.map.entrySet()) {
System.out.println(String.format("%s=%s", e.getKey(),e.getValue()));
}
}
- 出力結果
jsonString=[{"value":100,"string":"hogehoge","map":{"key2":"value2","key1":"value1"},"list":["item1","item2"]}]
100
hogehoge
item1
item2
key2=value2
key1=value1
JSON⇔Mapの相互変換
- コード
JsonObject jsonObject = JsonConverter.toObject(SAMPLE_JSON, JsonObject.class);
// Map⇒JSON文字列
Map<String, JsonObject> jsonMap = new HashMap<String, JsonObject>();
jsonMap.put("key1", jsonObject);
String jsonString = JsonConverter.toString(jsonMap);
System.out.println("jsonString=" + jsonString);
// JSON文字列⇒Map
Map<String, JsonObject> jsonMap2 = JsonConverter.toObject(jsonString,
new TypeReference<Map<String, JsonObject>>() {
});
for (Map.Entry<String, JsonObject> e : jsonMap2.entrySet()) {
JsonObject jsonObject2 = e.getValue();
System.out.println(jsonObject2.value);
System.out.println(jsonObject2.string);
for(String str : jsonObject2.list) {
System.out.println(str);
}
for(Map.Entry<String, String> e2 : jsonObject2.map.entrySet()) {
System.out.println(String.format("%s=%s", e2.getKey(),e2.getValue()));
}
}
- 出力結果
jsonString={"key1":{"value":100,"string":"hogehoge","map":{"key2":"value2","key1":"value1"},"list":["item1","item2"]}}
100
hogehoge
item1
item2
key2=value2
key1=value1