Can you build the array as an object before writing it, rather than bothering with all the individual pieces?
ObjectMapper mapper = new ObjectMapper();
ArrayNode array = mapper.createArrayNode();
int i = 0;
while (i < 6) {
array.add(mapper.createArrayNode().add("" + i++).add("" + i++));
}
System.out.println(array);
Results in:
[["0","1"],["2","3"],["4","5"]]
If you're not dealing with several megabytes of data or very tight memory constraints, this might turn out to be more maintainable as well.
Answer from Chris on Stack OverflowCan you build the array as an object before writing it, rather than bothering with all the individual pieces?
ObjectMapper mapper = new ObjectMapper();
ArrayNode array = mapper.createArrayNode();
int i = 0;
while (i < 6) {
array.add(mapper.createArrayNode().add("" + i++).add("" + i++));
}
System.out.println(array);
Results in:
[["0","1"],["2","3"],["4","5"]]
If you're not dealing with several megabytes of data or very tight memory constraints, this might turn out to be more maintainable as well.
JsonGenerator jg = new JsonFactory().createJsonGenerator(System.out);
jg.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
jg.writeStartArray();
int i = 0;
while (i < 6)
{
jg.writeStartArray();
jg.writeObject(i++);
jg.writeObject(i++);
jg.writeEndArray();
}
jg.writeEndArray();
jg.flush();
OUTPUT:
[["0","1"],["2","3"],["4","5"]]
Do you need a json like this...?
java - Creating a json object using jackson - Stack Overflow
java - How to parse a JSON string to an array using Jackson - Stack Overflow
java - How to Write Json Array to file using jackson - Stack Overflow
How to parse Json array with 2 or more different types using Jackson?
http://www.baeldung.com/jackson-inheritance
You'll need to have those types inherit from one base class though.
Also; that is really bad JSON.
More on reddit.comYou need a JsonNodeFactory:
final JsonNodeFactory factory = JsonNodeFactory.instance;
This class has methods to create ArrayNodes, ObjectNodes, IntNodes, DecimalNodes, TextNodes and whatnot. ArrayNodes and ObjectNodes have convenience mutation methods for adding directly most JSON primitive (non container) values without having to go through the factory (well, internally, they reference this factory, that is why).
As to an ObjectMapper, note that it is both a serializer (ObjectWriter) and deserializer (ObjectReader).
You can write an object to a json string. So I hope you have your data in an object of a class defined as per your need. Here is how you can convert that object into a json string:
//1. Convert Java object to JSON format
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(yourObject);
See here for the full jackson-databind javadoc.
I finally got it:
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));
The other answer is correct, but for completeness, here are other ways:
List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });
SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);
Jackson provide inbuilt methods for writing JSON data to JSON file. you can use these sort of code line for this
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("D:/cp/dataTwo.json"), jsonDataObject);
//new file(path of your file)
and jsonDataObject is your actual object(i.e object or array) which you want to write in file.
Write a list of objects to Json file:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.writeValue(new File("d:/temp/output.json"), studentList);
Where studentList is:
// object
Student student = new Student();
student.setActive(false);
student.setFirstName("Kir");
student.setId(123);
student.setLastName("Ch");
// list of objects
List<Student> studentList = List.of(student, student);
The result:
[ {
"id" : 123,
"firstName" : "Kir",
"lastName" : "Ch",
"active" : false,
"address" : null,
"languages" : null
}, {
"id" : 123,
"firstName" : "Kir",
"lastName" : "Ch",
"active" : false,
"address" : null,
"languages" : null
} ]