「Gradle」ObjectMapper クラスでJavaオブジェクトをjson文字列に変換
構文
ObjectMapper mapper = new ObjectMapper();
String json文字列= mapper.writeValueAsString(javaオブジェクト名);
使用例
1.build.gradle(gradleプロジェクトの設定ファイル)
dependencies {
// jackson-databindの導入
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0'
}
dependencies {
// jackson-databindの導入
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0'
}
dependencies { // jackson-databindの導入 implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0' }
2.クラスの定義
package com.arkgame.course;
public class User {
public int age;
public String username;
public String addr;
//get set略
}
package com.arkgame.course;
public class User {
public int age;
public String username;
public String addr;
//get set略
}
package com.arkgame.course; public class User { public int age; public String username; public String addr; //get set略 }
3.実行クラス(main)
package com.arkgame.course;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class ObjJsonDemo {
public static void main(String[] args) throws JsonProcessingException {
// Userのインスタンスの生成
User user = new User();
// クラスのメンバーの値を設定
user.username = "山田 太郎";
user.age = 32;
user.addr = "東京品川区";
// ObjectMapper変数の宣言
ObjectMapper mapper = new ObjectMapper();
// JSON文字列をインデントする
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// JavaオブジェクトをJSON文字列に変換
String res = mapper.writeValueAsString(user);
System.out.println("JSON文字列をインデントして表示:\n" + res);
}
}
package com.arkgame.course;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class ObjJsonDemo {
public static void main(String[] args) throws JsonProcessingException {
// Userのインスタンスの生成
User user = new User();
// クラスのメンバーの値を設定
user.username = "山田 太郎";
user.age = 32;
user.addr = "東京品川区";
// ObjectMapper変数の宣言
ObjectMapper mapper = new ObjectMapper();
// JSON文字列をインデントする
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// JavaオブジェクトをJSON文字列に変換
String res = mapper.writeValueAsString(user);
System.out.println("JSON文字列をインデントして表示:\n" + res);
}
}
package com.arkgame.course; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class ObjJsonDemo { public static void main(String[] args) throws JsonProcessingException { // Userのインスタンスの生成 User user = new User(); // クラスのメンバーの値を設定 user.username = "山田 太郎"; user.age = 32; user.addr = "東京品川区"; // ObjectMapper変数の宣言 ObjectMapper mapper = new ObjectMapper(); // JSON文字列をインデントする mapper.enable(SerializationFeature.INDENT_OUTPUT); // JavaオブジェクトをJSON文字列に変換 String res = mapper.writeValueAsString(user); System.out.println("JSON文字列をインデントして表示:\n" + res); } }
実行結果
JSON文字列をインデントして表示:
{
"age" : 32,
"username" : "山田 太郎",
"addr" : "東京品川区"
}
JSON文字列をインデントして表示:
{
"age" : 32,
"username" : "山田 太郎",
"addr" : "東京品川区"
}
JSON文字列をインデントして表示: { "age" : 32, "username" : "山田 太郎", "addr" : "東京品川区" }