Search code examples
javajsongeojsonjettison

Jettison JSON/java , send list of string with json request


I am creating JSON object and send over the network , like

 org.codehaus.jettison.json.JSONObject json = new org.codehaus.jettison.json.JSONObject();
                json.put("id", "15");
                json.put("code", "secret");
                json.put("type", "new type");

Also I have links of photos what I want to put into this JSON

my links like http://box.com/images/photo.jpg,http://box.com/images/photo1.jpg
http://box.com/images/photo2.jpg, http://box.com/images/photo3.jpg
As I understand I must have some list/array and put like
json.put("images", links)

How to do it, put and parse... I need one key, and list of values. Is JSON array is useful for this?

Thanks


Solution

  • Yes. JSONArray is what you need.

        List <String> links = getLinks();
        JSONArray array = new JSONArray();
        for (String link : links)
                array.put(link);
    
        JSONObject obj = new JSONObject();
        //put id, code, type...
        obj.put("images", array);