I need below kind of structure constructed in java and send it as response :
var abc = {
"action": "Remove",
"datatable": [
{ "userid": "userid0", "username": "name0" },
{ "userid": "userid1", "username": "name1" },
{ "userid": "userid2", "username": "name2" },
{ "userid": "userid3", "username": "name3" }
],
"msgType": "success"
};
I am doing:
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.add(jsonObj.toJSONString());
}
Map paramMap = new HashMap();
paramMap.put("action", "remove");
paramMap.put("datatable", jsonArray );
paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);
getJSONMessage(paramMap);
and here above function is converting paramMap into json string like:
public static String getJSONMessage(Map<String, String> paramMap) {
if (paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
but it is not creating the right structure, can anybody help me in this?
here is what I am getting output:
{"action":"Remove","datatable":[{\"userid\":\"userid0\",\"srcOfPhoto\":\"users\\\/JMMBGTCHG.jpg\",\"username\":\"name0\"}"],"msgType":"success"}
which is not being parsed in javascript.
var json = eval('(' + respTe开发者_如何学Pythonxt+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);
last alert showing undefined.
ohh sorry I forgot to paste last line , here is the last line:
getJSONMessage(paramMap);
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
JSONObject json = new JSONObject();
json.put("fromZIPCode","123456");
JSONObject json1 = new JSONObject();
json1.put("fromZIPCode","123456");
sList.add(json1);
sList.add(json);
System.out.println(sList);
Output will be
[{"fromZIPCode":"123456"},{"fromZIPCode":"123456"}]
package com.JsonArray;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class WriteJsonArray {
public static void main(String[] args) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action","Remove");
jsonObject.addProperty("msgType","success");
JsonObject item1 = new JsonObject();
item1.addProperty("userid", "userid0");
item1.addProperty("username","name0");
JsonObject item2 = new JsonObject();
item2.addProperty("userid", "userid1");
item2.addProperty("username","name1");
JsonObject item3 = new JsonObject();
item3.addProperty("userid", "userid2");
item3.addProperty("username","name2");
JsonObject item4 = new JsonObject();
item4.addProperty("userid", "userid3");
item4.addProperty("username","name3");
JsonArray jsonArray = new JsonArray();
jsonArray.add(item1);
jsonArray.add(item2);
jsonArray.add(item3);
jsonArray.add(item4);
jsonObject.add("datatable", jsonArray);
System.out.println("Result: \n" + jsonObject.toString());
}
}
org.json.simple.JSONArray resultantJson = new org.json.simple.JSONArray();
org.json.JSONArray o1 = new org.json.JSONArray("[{\"one\":[],\"two\":\"abc\"}]");
org.json.JSONArray o2 = new org.json.JSONArray("[{\"three\":[1,2],\"four\":\"def\"}]");
resultantJson.addAll(o1.toList());
resultantJson.addAll(o2.toList());
精彩评论