Trying to decode my fetch response:
fetch('restservices/users/', fetchOptions)
.then(function (response) {
if (response.ok) {
console.log('1');
console.log(response)
const responseSjon = response.json()
console.log('2');
console.log(responseSjon);
console.log('3');
// console.log(JSON.stringify(responseJson, null, 2));
console.log(JSON.stringify(responseJson));
console.log('4');
In my JavaScript, I parse the response as JSON using response.json. Then I log to my browser, and the (raw) response in the console is shown below: enter image description here
[
{
"email":{
"chars":"CasaSuCasa",
"string":"CasaSuCasa",
"valueType":"STRING"
},
"naam":{
"chars":"Coyote",
"string":"Coyote",
"valueType":"STRING"
},
"hoeveelheid zoekopdrachten":{
"integral":true,
"valueType":"NUMBER"
}
},
{
"email":{
"chars":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a19130e2a080b18180f1e1e44090507">[email protected]</a>",
"string":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72010b1632101300001706065c111d1f">[email protected]</a>",
"valueType":"STRING"
},
"naam":{
"chars":"CrazyDiamond",
"string":"CrazyDiamond",
"valueType":"STRING"
},
"hoeveelheid zoekopdrachten":{
"integral":true,
"valueType":"NUMBER"
}
}
]
Is it correct to assume it's an array with JsonArray objects, each containing JSON objects?
My question is how can I access these objects individually in a console.log(xxx); ?
EDIT: This is the code for the rest service on the other end, which I have access to:
Map<String, User> commune = Community.getUserMap();
JsonArrayBuilder jab = Json.createArrayBuilder();
JsonObjectBuilder job = Json.createObjectBuilder();
commune.forEach((key, user) -> {
job.add("email", user.getEmail());
job.add("naam", user.getNaam());
job.add("role", user.getRole());
job.add("hoeveelheid zoekopdrachten", user.getAlleZoekertjes().size());
jab.add(job);
});
return ok(jab.build()).build();
}