Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject

Tags:

json

arrays

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('(' + respText+')');
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 "";
}
like image 643
Ajka Avatar asked Sep 21 '11 10:09

Ajka


People also ask

How can we add a JSONObject to JSONArray in Java?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

What is the difference between JSONObject and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

How add JSON array to another JSON array?

We can merge two JSON arrays using the addAll() method (inherited from interface java. util.

How do I add a list to a JSON object?

How do I add a list to a JSON object? JSONObject obj = new JSONObject(); List sList = new ArrayList (); sList. add(“val1”); sList. add(“val2”); obj.


1 Answers

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

like image 91
Gonçalo Vieira Avatar answered Oct 09 '22 05:10

Gonçalo Vieira