Reputation: 1
I have a Json file that contains an Array of Json Objects:
[{"id":"939f0080-e93e-4245-80d3-3ac58a4a4335","name":"Micha","date":"2021-04-20T11:21:48.000Z","entry":"Wow"}, {"id":"939f0070-e93f-4235-80d3-3ac58a4a4324","name":"Sarah","date":"2021-04-21T11:21:48.000Z","entry":"Hi"}, {"id":"897f0080-e93e-4235-80d3-3ac58a4a4324","name":"John","date":"2021-04-25T17:11:48.000Z","entry":"Hi how are you"}...]
I'm using Json-simple to get the array, but I'm only able to get the object, but not the values.
JSONParser jsonParser = new JSONParser();
try {
FileReader reader = new FileReader("j.json");
Object object = jsonParser.parse(reader);
JSONArray jsonArray = (JSONArray) object;
//prints the first Object
System.out.println("element 1 is" + jsonArray.get(0));
//prints whole Array
System.out.println(jsonArray);
how do i Iterate trough my file and get the values of each date, name date and entry instead of the object?
I want to get something like :
"id is 939f0080-e93e-4245-80d3-3ac58a4a4335 name is Micha date is 2021-04-20T11:21:48.000Z enry is wow"
"id is 939f0070-e93f-4235-80d3-3ac58a4a4324 name is Sarah 2021-04-21T11:21:48.000Z date is 2021-04-21T11:21:48.000Z"
"name is ..."
Upvotes: 0
Views: 972
Reputation: 2998
What you want is basically this
public static void main(String[] args) {
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("j.json")) {
Object object = jsonParser.parse(reader);
JSONArray jsonArray = (JSONArray) object;
for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;
System.out.printf("id is %s name is %s date is %s entry is %s%n", jsonObject.get("id"), jsonObject.get("name"), jsonObject.get("date"), jsonObject.get("entry"));
// Or if you want all
for (Object key : jsonObject.keySet()) {
System.out.printf("%s is %s", key, jsonObject.get(key));
}
System.out.println();
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
You can use getOrDefault
in case the attribute is optional. There are also countless other libraries which can transform your json into a java object. This will give you more type safety. E.g. jackson
or gson
.
Upvotes: 1
Reputation: 76
Hope this helps:
JSONParser jsonParser = new JSONParser();
try {
Object object = jsonParser.parse("[{\"id\":\"939f0080-e93e-4245-80d3-3ac58a4a4335\",\"name\":\"Micha\",\"date\":\"2021-04-20T11:21:48.000Z\",\"entry\":\"Wow\"}, {\"id\":\"939f0070-e93f-4235-80d3-3ac58a4a4324\",\"name\":\"Sarah\",\"date\":\"2021-04-21T11:21:48.000Z\",\"entry\":\"Hi\"}, {\"id\":\"897f0080-e93e-4235-80d3-3ac58a4a4324\",\"name\":\"John\",\"date\":\"2021-04-25T17:11:48.000Z\",\"entry\":\"Hi how are you\"}]");
JSONArray jsonArray = (JSONArray) object;
jsonArray.forEach(x -> {
JSONObject o = (JSONObject) x;
String collect = o.entrySet()
.stream()
.map(e -> e.getKey() + " is " + e.getValue().toString())
.collect(Collectors.joining(" "));
System.out.println(collect);
});
} catch (ParseException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 782
JSONArray implements Collection and Iterable, so you can iterate over it with a For loop or using an Iterator or Stream. sadly, the object is not generic typed, so you will always get Objects and have to cast them yourself:
for(Object value : jsonArray) {
// your code here //
}
Upvotes: 0