2
 //create the JSON Object to pass to the client
 JSONObject object=new JSONObject();

 //one instance
 object.put("name","something2");
 object.put("status", "up");

 //second instance
 object.put("name", "something2");
 object.put("status", "down");

 String json = object.toString();  

 response.getOutputStream().print(json);

 System.out.println("JSON Contents: "+json);

所需的输出:{name:something1,status:up},{name:something2,status:down}...等

4

3 回答 3

2

你需要有 JSONArray :

JSONArray jsonarray = new JSONArray(); jsonarray.add(object)...
于 2011-06-20T13:43:42.450 回答
0

我建议不要使用无效的 JSON,而是{name: something1, status: up}, {name: something2, status: down}针对数组结构,例如[{name: something1, status: up}, {name: something2, status: down}]. 因此,您将使用net.sf.json.JSONArray、 添加JSONObjects、 构建它,类似于您已经拥有的东西。当然,您需要将其更改为两个不同JSONObjects的 ,每个都将元素“名称”和“状态”添加一次。

于 2011-06-20T13:44:16.837 回答
0

在这种情况下,您必须JSONArray改用。

List<Map> list = new ArrayList<HashMap>();
Map map1 = new HashMap();  
map1.put("name","something");  
Map map2 = new HashMap();  
map2.put("status", "up"); 
list.add(map1);
list.add(map2);

JSONArray array = JSONArray.fromObject(list);  
String json = array.toString();  
System.out.println("JSON: "+ json);
于 2011-06-20T13:44:24.433 回答