nidhi
nidhi

Reputation: 329

how to give a name to json object

how can i set a name to json object?

Map<String, ArrayList<String>> headerColumns = new LinkedHashMap<>();
headerColumns = gmailService.readMessgeAndProcess(session, MsgResponse, false, clientId);
JSONObject jsonObject = new JSONObject(headerColumns);

here jsonObject returns this output

[{"name":"aaaa","price":1133.43},
{"name":"bbbb","price":967.45}]

I tired to set the name to the jsonObject using jsonObject.put("products", jsonObject); but it doesn't work.

The expected output is

{"products": 
    [{"name":"aaaa","price":1133.43},
    {"name":"bbbb","price":967.45}]}

Upvotes: 2

Views: 1952

Answers (1)

Erik Lucio
Erik Lucio

Reputation: 948

Just nested the data into another Map:

Map<String, ArrayList<String>> headerColumns = new LinkedHashMap<>();
Map<String, Object> products= new LinkedHashMap<>();
headerColumns = gmailService.readMessgeAndProcess(session, MsgResponse, false, clientId);
products.put("products", headerColumns)
JSONObject jsonObject = new JSONObject(products);

Upvotes: 3

Related Questions