将JAVA对象转换为JSON [英] Convert JAVA Object to JSON

查看:1100
本文介绍了将JAVA对象转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我将对象转换为json时遇到问题.我正在使用gson将java对象转换为json字符串,但是对此我遇到了错误.以下是describe

我有班公共汽车

Hello everyone.

I have a problem with convert the object to json. I''m using gson to convert the java object to json string, but i got an error for this. Below is the describe

I have a class Bus

class Bus{
 private int id;
 private Route route;

  //setters and getters
}

class Route{
 private int id;
 private List<Bus> listBus;
 
 //setters and getters
}



当我使用gson将总线对象转换为json时,



When i using gson to convert the bus object to json,

Gson gson = new Gson();
Bus bus = getABus();//get the bus
String json = gson.toJson(bus);



它会产生此错误:



it generate this erros:

com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)



请问有人知道如何解决这个问题吗?

谢谢
Clark



Could anyone know how to solve this problem please?

Thanks
Clark

推荐答案

看起来您需要编写一个自定义序列化程序.查看指南: http://sites.google.com /site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization [
Looks like you need to write a custom serializer. Check out the guide: http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization[^]

Good luck!


这篇关于将JAVA对象转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆