You need to sign in to do that
Don't have an account?
Jan Revet
How to access field in object
I have this json string i am pulling in with the code below:
{"success":1,"statuscode":200,"data":[{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"},{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"},{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"}]}
The code in Developer Console:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://domain.ext/test');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
List<Object> rows = (List<Object>) results.get('data');
for (Object row : rows) {
System.debug('TEST ' + row.kleur1);
}
}
This code produces an error: Line: 13, Column: 36
Variable does not exist: kleur1. Houw should I access the field 'kleur1' of the object row?
{"success":1,"statuscode":200,"data":[{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"},{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"},{"kleur1":"rood","kleur2":"wit","kleur3":"blauw"}]}
The code in Developer Console:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://domain.ext/test');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
List<Object> rows = (List<Object>) results.get('data');
for (Object row : rows) {
System.debug('TEST ' + row.kleur1);
}
}
This code produces an error: Line: 13, Column: 36
Variable does not exist: kleur1. Houw should I access the field 'kleur1' of the object row?
for this work around you replace for loop code with below code
//access json data
for (Object row : rows) {
Map<String, Object> rw = (Map<String, Object>) row;
System.debug('TEST ' + rw.get('kleur1'));
}
All Answers
for this work around you replace for loop code with below code
//access json data
for (Object row : rows) {
Map<String, Object> rw = (Map<String, Object>) row;
System.debug('TEST ' + rw.get('kleur1'));
}