简体   繁体   English

在Java中,如何在单个JSON对象中表示多个(相同类型的)对象

[英]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";

using json lib 使用json lib

List mybeanList = new ArrayList();
mybeanList.add(myBean1);
mybeanList.add(myBean2);

JSONArray jsonArray = JSONArray.fromObject(mybeanList);

You can also use XStream to do this 您也可以使用XStream执行此操作


See Also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Java DTO将嵌入式JSON对象表示为字符串 - How do I represent a embedded JSON object as a string via the Java DTO 如何解析包含多个相同类型的 JSON 对象(不是数组)的 JSON object - How to Parse a JSON object with contains multiple JSON objects ( not an array) of the same type 如何使用字符串作为参数来表示Java中的对象? - How do I use a string as an argument to represent an object in Java? 如何使用Java将具有相同状态的多个对象发送到具有单个Object参数的方法? - How to send multiple objects with same state to a method with single Object parameter using Java? java - 如何跨多个类传递相同的对象java? - How do I pass the same object across multiple classes java? 在JAVA中将JSON表示为对象数组 - Represent JSON as an array of objects in JAVA 如何遍历包含与对象具有相同类型的子对象的对象? - How do I iterate through an object which contain children objects with the same type as the object? 如何在单个POJO中表示Hibernate对象的集合? - How do you represent collections of Hibernate objects in a single POJO? 如何从 Java 中的单个文件读取多个 JSON 对象? - How to read multiple JSON objects from a single file in Java? 如何以JSON格式表示数据? - How do I represent the data in a JSON format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM