Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a List to Json ObjectNode

Tags:

java

json

ObjectNode row = Json.newObject();
row.put("0", a);
row.put("1", x);
row.put("2", y);

now I have list

List<String> list = new ArrayList<String>();

How can I add this to the row?

like image 982
atul mishra Avatar asked Dec 12 '15 13:12

atul mishra


People also ask

How do you pass a list in ObjectNode?

You can use the putArray method which creates an ArrayNode . Then you should fill it with the elements from your list. addAll expects an ArrayNode or a liste of JsonObjects, you'll want to iterate through your list to add its strings to the new ArrayNode. @EricMaziade, you are right.

What is the difference between JsonNode and ObjectNode?

JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.


1 Answers

You can use the putArray method which creates an ArrayNode. Then you should fill it with the elements from your list.

ArrayNode arrayNode = row.putArray("myList");
for (String item : list) {
    arrayNode.add(item);
}
like image 119
fracz Avatar answered Sep 24 '22 12:09

fracz