我已经能够使用import org.json.JSONObject从servlet发出一个json对象; 请参阅下面的代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
// put a "person"
JSONObject person = new JSONObject();
try {
person.put("age", "24");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
person.put("gender", "M");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
person.put("ID", "10082");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
json.put("person", person);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// finally output the json string
out.print(json.toString());
}
这给了我一个json响应,如:
{"person":{"age":"24","gender":"M","ID":"10082"}}
但是,我想得到一个包含3个json对象的数组。我怎么能得到它?
[
{
"age": "21",
"gender": "M",
"ID": "10092"
},
{
"age": "25",
"gender": "M",
"ID": "10091"
},
{
"age": "24",
"gender": "M",
"ID": "10095"
}
]
答案 0 :(得分:2)
您必须为每个用户创建不同的JSON对象,然后将所有这些对象放入JSON数组中。最后将数组添加到主JSON对象。您可以从以下代码中获得想法(我假设您的数据位于某些ArrayList中,例如All_Users。):
JSONObject main_JSON_Object =new JSONObject();
JSONArray main_JSON_Array=new JSONArray();
for(int i= 0;i<All_Users.size();i++)
{
JSONObject user_Object=new JSONObject();
try
{
user_Object.put("person", All_Users.get(i));
}
catch(NullPointerException ex)
{
}
main_JSON_Array.put(user_Object);
}
main_JSON_Object.put("userarray", main_JSON_Array);
答案 1 :(得分:0)
使用JSONArray和JSONObject
JSONObject objJSON =new JSONObject();
JSONArray arrJSON =new JSONArray();
while(resultset.next()){ // put a "person" JSONObject person = new JSONObject(); try { person.put("age", "24"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { person.put("gender", "M"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { person.put("ID", "10082"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { arrJSON .add(person); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } objJSON.put("person",arrJSON );
如果您在使用objJSON.put("person",arrJSON );
时遇到任何异常,请尝试使用此objJSON.put("person",(Object)arrJSON );
。
你会得到一个像你这样的结构。
{"person" : [{"age":"24","gender":"M","ID":"10082"},{"age":"25","gender":"F","ID":"10083"},{"age":"26","gender":"O","ID":"10084"}]
使用地图和列表
LinkedList listOfPerson = new LinkedList();
while(resultset.next()){
// put a "person"
Map person = new HashMap();
try {
person.put("age", "24");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
person.put("gender", "M");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
person.put("ID", "10082");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
listOfPerson.add(person);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
objJSON.put("person",listOfPerson);