简体   繁体   中英

convert java object to json

In Java I have object of this kind Map<Member, Map<CustomerVO, Set<Vehicle>>> and one other List<DeviceOrder> I wanted to convert this to json, I tried with Gson but it is throwing error as "Forgot to register a type adapter?"

can someone please help how I do proceed, if not gson what would be other way to convert.

The above error was when I tried this way

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

for the other one as

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

Please let me know what am I doing wrong. Later I want to pass this to jsp to display.

UPDATE: Even tried this way as well, but no use.

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

You may try out the standard implementation of the Java API for JSON processing which is part of J2EE .

For your List<DeviceOrder> devLst , define the 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;
}

And then use:

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

The output should be:

[
    {
        "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"
    }
]

Your Map<Member, Map<CustomerVO, Set<Vehicle>>> map can be reduced to a List and JSONed in a similar fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM