Your root JSON is an Array, so first create a JSONArray from your String.
Do this:
JSONArray arr = new JSONArray(jstring);
for (int i = 0; i < arr.length(); i++) { // Walk through the Array.
JSONObject obj = arr.getJSONObject(i);
JSONArray arr2 = obj.getJSONArray("fileName");
// Do whatever.
}
For more info, please refer to the docs on JSONArray and JSONObject.
Your root JSON is an Array, so first create a JSONArray from your String.
Do this:
JSONArray arr = new JSONArray(jstring);
for (int i = 0; i < arr.length(); i++) { // Walk through the Array.
JSONObject obj = arr.getJSONObject(i);
JSONArray arr2 = obj.getJSONArray("fileName");
// Do whatever.
}
For more info, please refer to the docs on JSONArray and JSONObject.
You have to directly construct JSONArray from JSON string in this case.
JSONArray arr = new JSONArray(jstring);
How to convert JSON string into List of Java object? - Stack Overflow
How to turn json objects into an array of json objects using Java?
api design - Representing a large list of complex objects in JSON - Software Engineering Stack Exchange
java - JSON Structure for List of Objects - Stack Overflow
Videos
You are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
For any one who looks for answer yet:
1.Add jackson-databind library to your build tools like Gradle or Maven
2.in your Code:
ObjectMapper mapper = new ObjectMapper();
List<Student> studentList = new ArrayList<>();
studentList = Arrays.asList(mapper.readValue(jsonStringArray, Student[].class));
Hi. I'm new to java and stuck wondering how I would go about turning something like this: {} {} {} into this: [{},{},{}]. I'm trying to figure out how to do this to my incoming json data using Java. The data has like 20 similar objects, with the same keys but with constantly changing values.
Any help is much appreciated.
Sincerely,
your friendly neighborhood noob
Lean towards option 1, as it's a more expected format.
Option 1 works with JSON as it's designed to be used and therefore benefits from what JSON offers (a degree of human readability, which is good for debugging, and straightforward parsing, which is good for limiting entire categories of bugs to begin with).
Option 2 begrudgingly adopts JSON and subverts many of the benefits. If you don't want human readability, use protobuf or something similar... AIWalker's "CSV"-like approach isn't terrible either. It is marginally better (readable) than splitting objects apart and recombining them. But, this is still not as good (readable) as using JSON "as designed".
Also bear in mind, your API responses are also likely going to be gzipped. Most of the repetition in option 1 will be quickly and transparently condensed over the wire.
As an aside, if you're moving a lot of data, also consider JSONL or paginated results. Pagination can be especially helpful for web clients, as it places natural pauses in the processing, providing a degree of "organic" protection against UI lockups.
A list of objects is easier to work with. You can use append, map, filter... All the nice things JS Arrays have which manual indexing doesn't. And there's no way to get out of sync, so that's an entire class of bugs gone.
If you're worried about efficiency:
- Measure (premature optimization is the root of all evil)
- Consider the list of lists trick AIWalker proposed
- Consider an outright binary format
- Make sure gzip is enabled
- Measure (it's worth saying twice)
The second is almost correct:
Copy{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
The first example from your question,
Copyfoos: [
foo: { ... },
foo: { ... }
]
is in invalid syntax. You cannot have object properties inside a plain array.
The second example from your question,
Copyfoos: [
{ ... },
{ ... }
]
is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.
Following is the correct one when you want to obey strict JSON:
Copy"foos": [
{ ... },
{ ... }
]
This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.