如何使用net.sf.json在单个JSON对象中放置多个JSON实例。JSONObject图书馆

时间:2022-07-04 19:37:16
 //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);

Desired output: {name: something1, status: up}, {name: something2, status: down}... etc

期望输出:{name: something1, status: up}, {name: something2, status: down}…等

3 个解决方案

#1


2  

You need to have JSONArray :

你需要有JSONArray:

JSONArray jsonarray = new JSONArray(); jsonarray.add(object)...

#2


0  

Instead of {name: something1, status: up}, {name: something2, status: down}, which is not valid JSON, I recommend targeting an array structure, such as [{name: something1, status: up}, {name: something2, status: down}]. So, you'd build this with net.sf.json.JSONArray, adding JSONObjects, built similarly to what you've already got. Of course, you'd need to change it to make two different JSONObjects, each of which would have the elements "name" and "status" added only once each.

不是{name: something1, status: up}, {name: something2, status: down},这不是有效的JSON,我建议针对一个数组结构,比如[{name: something1, status: up}, {name: something2, status: down}]。因此,您可以使用net.sf.json构建它。JSONArray添加了JSONObjects,构建方式与您已经获得的类似。当然,您需要更改它,以创建两个不同的jsonobject,每个jsonobject只添加一次元素“name”和“status”。

#3


0  

In this case you'd have to use JSONArray instead.

在这种情况下,您将不得不使用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);

#1


2  

You need to have JSONArray :

你需要有JSONArray:

JSONArray jsonarray = new JSONArray(); jsonarray.add(object)...

#2


0  

Instead of {name: something1, status: up}, {name: something2, status: down}, which is not valid JSON, I recommend targeting an array structure, such as [{name: something1, status: up}, {name: something2, status: down}]. So, you'd build this with net.sf.json.JSONArray, adding JSONObjects, built similarly to what you've already got. Of course, you'd need to change it to make two different JSONObjects, each of which would have the elements "name" and "status" added only once each.

不是{name: something1, status: up}, {name: something2, status: down},这不是有效的JSON,我建议针对一个数组结构,比如[{name: something1, status: up}, {name: something2, status: down}]。因此,您可以使用net.sf.json构建它。JSONArray添加了JSONObjects,构建方式与您已经获得的类似。当然,您需要更改它,以创建两个不同的jsonobject,每个jsonobject只添加一次元素“name”和“status”。

#3


0  

In this case you'd have to use JSONArray instead.

在这种情况下,您将不得不使用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);