我一直在尝试评估GWT Autobean功能,以便将JSON对象解码/编码为域对象,以便进行REST调用。
遵循下面的示例:http://code.google.com/p/google-web-toolkit/wiki/AutoBean#Quickstart
我能够将一个单独的JSON对象转换为域对象:
AutoBean<Person> personBean = AutoBeanCodex.decode(factory, Person.class, JsonResources.INSTANCE.json().getText());其中JsonResources.INSTANCE.json()返回一个JSON字符串。
但是,我还没有成功地将Person对象列表从JSON转换过来。
如果有人有这样的例子,那会很有帮助的?
谢谢!
发布于 2012-12-01 03:19:30
我能想到的唯一方法就是创建一个特殊的autobean,它将具有List<Person>属性。例如:
public interface Result {
void setPersons(List<Person> persons);
List<Person> getPersons();
}和示例json字符串:
{
persons:[
{"name":"Thomas Broyer"},
{"name":"Colin Alworth"}
]
}更新:当input JSON是一个数组(正如persons[0]在注释中建议的那样).E.g时的解决方法。JSON看起来像这样:
[{"name":"Thomas Broyer"},{"name":"Colin Alworth"}]解析代码如下所示:
AutoBeanCodex.decode(factory, Result.class, "{\"persons\": " + json + "}").getPersons();https://stackoverflow.com/questions/13651068
复制相似问题