将java对象转换为json

时间:2012-05-22 16:03:14

标签: java json gson

在Java中,我有这种对象Map<Member, Map<CustomerVO, Set<Vehicle>>>和另一个List<DeviceOrder>我想将其转换为json,我尝试使用Gson但是它会抛出错误“忘了注册一个类型适配器?“

有人可以帮助我如何继续进行,如果不是gson将会有什么其他方式进行转换。

上述错误是我尝试这种方式

List<DeviceOrder> devLst;
Gson gson = new Gson();
String jsonStr = gson.toJson(devLst);

另一个

Map<Member, Map<CustomerVO, Set<Vehicle>>> map;
Gson gson = new Gson();
String jsonStr = gson.toJson(map);

请让我知道我做错了什么。后来我想将它传递给jsp进行显示。

更新 甚至尝试过这种方式,但没有用。

Gson gson = new Gson();
Type type = new TypeToken<List<DeviceOrder>>(){}.getType();
String jsonstr = gson.toJson(devLst, type);

1 个答案:

答案 0 :(得分:0)

您可以尝试Java API for JSON processingJ2EE的标准实现。

对于List<DeviceOrder> devLst,定义bean:

public class DeviceOrder implements Serializable {
    private static final long serialVersionUID = 893438341L;

    public DeviceOrder() {
    }

    public DeviceOrder(int id, String desc, Date date) {
        this.id = id;
        this.desc = desc;
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        result = prime * result + ((desc == null) ? 0 : desc.hashCode());
        result = prime * result + id;
        return result;
    }

    private int id;
    private String desc;
    private Date date;
}

然后使用:

final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

List<DeviceOrder> devLst = new ArrayList<DeviceOrder>() {
    {
        add(new DeviceOrder(1, "order 1", sdf.parse("2010-05-01")));
        add(new DeviceOrder(2, "order 2", sdf.parse("2010-06-01")));
        add(new DeviceOrder(3, "order 3", sdf.parse("2010-07-01")));
    }
};
DeviceOrder[] devArr = devLst.toArray(new DeviceOrder[devLst.size()]);

JsonArrayBuilder devArrBuilder = Json.createArrayBuilder();
for (DeviceOrder devOrder : devArr) {
    JsonObjectBuilder jsonObject = Json.createObjectBuilder()
            .add("id", devOrder.getId())
            .add("desc", devOrder.getDesc())
            .add("date", sdf.format(devOrder.getDate()));
    devArrBuilder.add(jsonObject);
}
JsonArray jsonArray = devArrBuilder.build();

Map<String, Object> prop = new HashMap<String, Object>() {
    {
        put(JsonGenerator.PRETTY_PRINTING, true);
    }
};
JsonWriter jsonWriter = Json.createWriterFactory(prop).createWriter(System.out);
jsonWriter.writeArray(jsonArray);
jsonWriter.close();

输出应为:

[
    {
        "id":1,
        "desc":"order 1",
        "date":"2010-05-01"
    },
    {
        "id":2,
        "desc":"order 2",
        "date":"2010-06-01"
    },
    {
        "id":3,
        "desc":"order 3",
        "date":"2010-07-01"
    }
]

您的Map<Member, Map<CustomerVO, Set<Vehicle>>> map可以以类似的方式缩减为List和JSONed。