[英]In Java, How do I represent multiple objects (of same type) in a single JSON object
I need to pass the attribtutes of particular type, as apart of a restful service to a javascript which will then display them to a webpage 我需要将特定类型的属性作为静态服务的一部分传递给javascript,然后将其显示在网页上
@GET
@Produces("application/json")
@Consumes("application/json")
@Path("/getStatusAll")
public void getStatusAll(
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws ServletException,
IOException
{
JSONArray jArray = new JSONArray();
Collection<S> s = Manager.getS().values();
for (Server i : svr)
{
JSONObject m = new JSONObject();
m.put("name",i.getName());
m.put("status",i.getStatus());
jArray.add(m);
}
return jArray.toString();
response.getOutputStream().print(jArray);
response.flushBuffer();
}
JAVASCRIPT will need to read ONE JSON object looking like: JAVASCRIPT将需要读取一个JSON对象,如下所示:
[ {name:someName0, status: someStatus},
{name:someName1, status: someStatus},
{name:someName2, status: someStatus}...etc]
What you're doing is the correct way to do it. 您正在执行的操作是正确的方法。 JSON calls always return one object. JSON调用始终返回一个对象。
The object you got back is an array, so you can extract the elements like this: 返回的对象是一个数组,因此您可以提取以下元素:
var myResponse = makeJSONCall();
// You have received myResponse.length objects, you can bind them
// to variables if you want
var thingOne = myResponse[0];
var thingTwo = myResponse[1];
...
// You can use them from their variable names or straight from the array
thingOne.name = "Joe Bob";
myResponse[3].status = "Tired";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.