ArrayList<Object> JSON

前端 未结 4 1836
执念已碎
执念已碎 2021-01-22 10:10

I am trying to return JSON data with my restlet. I can return a single item\'s JSON with..

import org.json.JSONObject;

Site aSite = new Site().getSite();   
JSO         


        
4条回答
  •  渐次进展
    2021-01-22 11:02

    here is my solution using simple-json.

    JSONArray jr = new JSONArray();
    for (int x = 1; x <= number_of_items; x++)
        {
            JSONObject obj = new JSONObject();
            obj.put("key 1", 10);
            obj.put("key 2", 20);
            jr.add(obj);
    
        }
    System.out.print(jr);
    

    Output:

    [{"key 1":10,"key 2":20},{"key 1":10,"key 2":20}]
    

提交回复
热议问题