JSON数据问题
我正在实施 RESTful Jersey 应用程序。
如果我有一个car bean:
@XmlRootElement
class Car{
private String id;
private String name;
//... GETTER & SETTER
}
My CarResource.java:
@GET
@Produces({MediaType.APPLICATION_JSON})
public List<Car> getCar(@QueryParam("id") String id) {
List cars= new ArrayList<Car>();
cars.add(new Car(id, "my car 1"));
return cars;
}
如果cars
列表中只有一个元素,我得到的JSON数据是
{"car":{"id":"12","name":"my car 1"}}
但我需要的是(用“[]”):
{"car":[{"id":"12","name":"my car 1"}]}
怎么办?
PS如果cars
列表中有多个元素,则JSON数据确实有“[]”。
I am implementing RESTful Jersey application.
If I have a car bean:
@XmlRootElement
class Car{
private String id;
private String name;
//... GETTER & SETTER
}
My CarResource.java:
@GET
@Produces({MediaType.APPLICATION_JSON})
public List<Car> getCar(@QueryParam("id") String id) {
List cars= new ArrayList<Car>();
cars.add(new Car(id, "my car 1"));
return cars;
}
If there is only one element in the cars
list, the JSON data I got is
{"car":{"id":"12","name":"my car 1"}}
But what I need is (with "[ ]"):
{"car":[{"id":"12","name":"my car 1"}]}
How to do?
P.S. if there are more than one element in the cars
list, the JSON data do have "[ ]".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须处理这个 JavaScript 端。
假设您已将数据解析为 JavaScript 对象,请尝试类似以下操作来获取项目:
您可以在 这个 JSFiddle
You'll have to handle this JavaScript-side.
Assuming you've parsed your data into a JavaScript object, try something like the following to get the items:
You can see it in action at this JSFiddle
我在javascript中使用以下脚本来解析json数据(即使它有一条或多条记录)
https:// /github.com/douglascrockford/JSON-js
如果您可以转换该脚本中使用的核心代码进行解析,那么您可以处理这种情况
I use the following script in javascript to parse the json data (even if it has one or more records)
https://github.com/douglascrockford/JSON-js
If you can convert the core code used in that script to parse then u can handle that situation