0
@GET
@Produces("application/json")
@Consumes("application/json")
@Path("/getStuff/{id}")
public String getStuff(
@PathParam("id") String id,
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws Exception
{
Collection<Stuff> stuff = Manager.getStuff().values();
JSONArray jArray = new JSONArray();
for (Stuff i : stuff)
{
jsnObjct.put("id", i.getId());
jsnObjct.put("name", i.getName());
jArray.add(jsnObjct);
}
json = jArray.toString();
response.setContentType("text/javascript");
response.getOutputStream().print(json);
response.flushBuffer();
return null;
}
为什么在将它们放入JSON数组之前排序Java对象?我认为你的循环应该是'for(Stuff i:stuff)' – Paul
JSONObjects不具有可比性。那么,按什么分类? –
@James,他可以根据自己的意愿进行排序。在迭代之前,他可以对他的'Collection'进行分类。例如,他可以将每个'Stuff'拆分为一个'TreeMap',其中key ='getId()'和val ='getName()',然后遍历TreeMap并构建他的JSON数组。他可以扩展'JSONObject'类并使其具有可比性。 –
Paul