2

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!!

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

2 Answers 2

10

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));
}
1
  • Yep works. But holy guacamole, for loops instead of lamba expressions. Apex ftw...
    – Shanerk
    Commented Dec 13, 2018 at 0:10
0
List<Object> myStringObjects = new List<Object>{'a', 'b', 'c'};

List<String> myStrings = String.join(myStringObjects, ';').split(';');

We can go future and create method:

public List<String> convert(Object iterableObject) {
    return String.join(new List<Object>(iterableObject), ';').split(';');
}

List<String> myStringsFromSet = convert(new Set<Object>{'a', 'b', 'c'});

List<String> myStringsFromList = convert(new List<Object>{'a', 'b', 'c'});

Pros:

  • Code is very simple

Cons:

  • Be careful with the separator (in my example ;), because if your strings contain it, it will be working not as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .