次のコードが機能しています
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, DataObject> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, DataObject>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, DataObject> getData() {
return data;
}
public void addData(final String key, DataObject value, Class<T> t) {
data.put(key, value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}
出力:
{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{"data1":32,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}}}}
しかし、私は実際にジェネリックで作業を行う必要があります
Map<String, Object> data
したがって、このコード:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, Object> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, Object>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, Object> getData() {
return data;
}
public void addData(final String key, Object value, Class<T> t) {
data.put(key, value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}
これは機能していません:出力:
{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{}}}
Gsonは一般的なオブジェクトを処理できないため、myOtherDataオブジェクトがありません
そのため、addDataメソッドの3番目の引数としてクラスを配置し始めました。
Map<String, <T>> data;
マップで構成可能なタイプを宣言する方法がわかりません
その仕事をしてくれる人に感謝します
編集:それはほとんどそのように働いています
スレッド「main」でエラー例外が発生しますjava.lang.ClassCastException:DataObjectをDataObject.addData(test.java:41)のjava.lang.Classにキャストできません
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, Class<T>> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, Class<T>>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, Class<T>> getData() {
return data;
}
public void addData(final String key, Object value, Class<T> t) {
data.put(key, (Class<T>) value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}