使用Object的Java Gson编码不起作用

时间:2012-04-25 12:56:17

标签: java gson

以下代码正在运作

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无法使用常规Object

这就是为什么我开始把这个类作为addData方法的第三个参数,我需要把

Map<String, <T>> data;

我不知道在地图中声明可配置类型的方法

感谢任何能够开展这项工作的人


编辑:它几乎像那样工作

我收到了错误 线程“main”中的异常java.lang.ClassCastException:无法将DataObject强制转换为java.lang.Class     在DataObject.addData(test.java:41)

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));
    }

}

1 个答案:

答案 0 :(得分:0)

编辑:  删除addData()方法的class参数,不需要它。只需直接使用T输入值,然后使用data输入Map<String, T>。使用以下内容初始化地图:this.data = new HashMap<String, T>();


这对我有用(我用T键入你的Map值并删除了addData的unncesary Class参数)。但是你的代码实际上没有那些修改。我在我的机器上运行它,并且输出与你标记为“工作”的输出相同。

有点奇怪,是你输入DataObject。这很令人困惑,因为你还有一个同一类的内部成员(子)。我不知道它是否应该是同一类型DataObject<T>。你应该尝试尽可能正确地输入你的泛型。如果你不能这样做,可能是一些错误使用泛型的迹象。

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 List<String> list = new ArrayList<String>() {
        {
            add("String 1");
            add("String 2");
            add("String 3");
        }
    };
    private int data1 = 100;
    private String data2 = "hello";
    DataObject child;
    private Map<String, T> data = null;

    public DataObject(int i) {
        this.data1 = i;
        this.data = new HashMap<String, T>();
    }

    // getter and setter methods

    @Override
    public String toString() {
        return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list=" + list + "]";
    }

    public Map<String, T> getData() {
        return data;
    }

    public void addData(final String key, T value) {
        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));
        Gson gson = new Gson();
        System.out.println(gson.toJson(obj));
    }

}