+ Start a Discussion
Jan RevetJan 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?
Best Answer chosen by Jan Revet
Ahmed Ansari 13Ahmed Ansari 13


    
  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

Ahmed Ansari 13Ahmed Ansari 13


    
  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'));
    }

This was selected as the best answer
Jan RevetJan Revet
Thanks Ahmed, it works!