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

前端 未结 4 830
梦谈多话
梦谈多话 2020-12-16 14:27

I need below kind of structure constructed in java and send it as response :

var abc = {
  "action": "         


        
相关标签:
4条回答
  • 2020-12-16 15:05
    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

    0 讨论(0)
  • 2020-12-16 15:05
    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"}]
    
    0 讨论(0)
  • 2020-12-16 15:13
    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());
    
    0 讨论(0)
  • 2020-12-16 15:20
    JSONArray successObject=new JSONArray();
    JSONObject dataObject=new JSONObject();
    successObject.put(dataObject.toString());
    

    This works for me.

    0 讨论(0)
提交回复
热议问题