Leandro
Leandro

Reputation: 51

JSONArray with Jettison weird printout

I am using jettison to generate a json in my webservice. I need to create an array and just for test purpose, I am using this little program below:

public static void main(String[] args) throws Exception {
        JSONObject root = new JSONObject();
        JSONArray array = new JSONArray();
        for (int i=0; i<5; i++) {
            JSONObject o = new JSONObject();
            array.put(o);
            o.put("name", "leandro-" + i);
            o.put("age", i*2);
        }
        root.put("Nomes", array);
        System.out.println(root);

    }

But, the output is:

{"Names":"[\"{\\"name\\":\\"leandro-0\\",\\"age\\":0}\",\"{\\"name\\":\\"leandro-1\\",\\"age\\":2}\",\"{\\"name\\":\\"leandro-2\\",\\"age\\":4}\",\"{\\"name\\":\\"leandro-3\\",\\"age\\":6}\",\"{\\"name\\":\\"leandro-4\\",\\"age\\":8}\"]"}

I would like to have the following, clear, string:

{"Names":[{"name":"leandro-0","age":0},{"name":"leandro-1","age":2},...]}

The question is: Why I am getting the weird json with double quotes and several backslashs? What am I doing wrong?

Tks in advance. Leandro

Upvotes: 0

Views: 110

Answers (0)

Related Questions