以下代码正在运行
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":{}}}
缺少 myOtherData 对象,因为 Gson 无法与通用对象一起使用
这就是为什么我开始把类作为 addData 方法的第三个参数,我需要把
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));
}
}