Reputation: 329
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
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