The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.
As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
For reference, see "Example 1" on the json-simple decoding example page.
Answer from Greg Kopff on Stack OverflowHow to read json file into java with simple JSON library - Stack Overflow
Useful JSON viewer โ JviewSON
JSON Editor Online - view, edit and format JSON online
Looking for a Java decompiler
What browser should I use to view JSON files?
Is it safe to view JSON files using free GroupDocs Viewer?
How can I open and view JSON file?
Videos
The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.
As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
For reference, see "Example 1" on the json-simple decoding example page.
You can use jackson library and simply use these 3 lines to convert your json file to Java Object.
ObjectMapper mapper = new ObjectMapper();
InputStream is = Test.class.getResourceAsStream("/test.json");
testObj = mapper.readValue(is, Test.class);
Hey everyone!
I recently made a little tool called JviewSON, and I thought it might come in handy for anyone who works with JSON files regularly. Itโs simple and designed for viewing and analyzing JSON files without any unnecessary extras.
A few key features:
Displays JSON data in a clean and easy-to-read structure.
Built-in search to help you find what you need fast.
Automatically updates if the JSON file changes in the background.
Itโs view-only, so no risk of accidentally editing your data.
If youโre curious, you can check it out on itch.io, where you can also run a browser-based version without downloading:
https://pocdev.itch.io/jviewson
Or if you prefer to dig into the code (made in Godot) hereโs the GitHub repo:
https://github.com/andreas-volz/JviewSON
Would love to hear what you think or if you have any ideas for improvement!