1

Here is my apex code"

apex clas

public class GetRestfulExampleSu{

public string jsonStr {get;set;}
public Pagereference getJSONFromREST() {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://dz.co.rplug.renault.com/localsemiclair/BAWn'); 
        req.setHeader('Accept','application/JSON');
        req.setMethod('GET');
        HttpResponse res = h.send(req); 
        jsonStr= res.getBody();

        system.debug('jsonStr>>>>-->>>>>>'+jsonStr);

        jsonStr= res.getBody();

        Map<String,Object> rawObj = (Map<String,Object>) JSON.deserializeUntyped(jsonStr);

   Map<String,Object> responseObj = (Map<String,Object>)rawObj.get('localSemiClair');
   Map<String,Object> responseObjinside2 = (Map<String,Object>)responseObj.get('mapRepresentation'); 
   Map<String,Object> responseObjinside3 = (Map<String,Object>)responseObjinside2 .get('map');   
   List<Object> reqs = (List<Object>) responseObjinside3 .get('map');
   List<string> lsstr= new List<string> ();

   for(Object a:reqs){
     lsstr=(List<string>)a;  
   }


      return null;
 }

}

I want to convert "reqs" value into a list of string. It showing error.Please help me out.Thanks in advance!!

  • instead of lsstr=(List<string>)a; use lsstr=(String)a; – kurunve Jan 25 '16 at 9:22
  • what is the error you are getting? – Nitin Jan 25 '16 at 9:24
  • System.NullPointerException: Attempt to de-reference a null object – uma451 Jan 25 '16 at 9:27
  • 1
    instead of lsstr=(List<string>)a; use lsstr.add(String.valueOf(a)); – Peeyush Jan 25 '16 at 9:37
  • @uma If this worked for you then please mark my answer as correct – Peeyush Jan 25 '16 at 11:56
9

Instead of this code:

List<Object> reqs = (List<Object>) responseObjinside3 .get('map');
List<string> lsstr= new List<string> ();

for(Object a:reqs){
   lsstr=(List<string>)a;  
}

Use this code:

List<Object> reqs = (List<Object>) responseObjinside3.values();
List<string> lsstr= new List<string> ();

for(Object a: reqs){
   lsstr.add(String.valueOf(a));
}
| improve this answer | |
  • Yep works. But holy guacamole, for loops instead of lamba expressions. Apex ftw... – Shane Dec 13 '18 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.