1

This is my apex code

public PageReference find() {         

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    String url = 'http://clozer.3spire.net/public/user/'+strTag+'/'+cities;

    req.setEndpoint(url);
    req.setMethod('GET');

    //these parts of the POST you may want to customize
    req.setCompressed(false);
    req.setBody('key1=value1&key2=value2');
    req.setHeader('Content-Type', 'application/json');  

    try {
        res = http.send(req);       
    } catch(System.CalloutException e) {
        system.debug('Callout error: '+ e);
        //result = ''+e;
    }    

    data = (Map<String, Result>)JSON.deserialize(res.getBody(),Map<String, Result>.class);

    lstResultWrapper = new List<ResultWrapper>();
    for(Result obj: data.values())
    {      
      lstResultWrapper.add(new ResultWrapper(obj));
    }

        return null;
    }     

public class Result
{
    public String description {get;set;}
    public String person_name {get;set;}
    public String email {get;set;}
    public String exparience {get;set;}
    public String location {get;set;}
}

public class ResultWrapper{

 public Result objResult {get;set;}
 public Boolean isSelected {get;set;}

 public ResultWrapper(Result objResult){
   this.objResult = objResult;
   isSelected = false;
 }    
}  

Json string is

{"0":{"description ":"","person_name":"Justin Townsley","email":"jlt@staranchor.com","exparience":null,"location":"Unknown"},"1":{"description ":"","person_name":"Dan Anderson","email":"danandersonmobile@yahoo.com","exparience":null,"location":"Unknown"},"2":{"description ":"","person_name":"Artak Robert Melkonyan","email":"artakrobert@gmail.com","exparience":null,"location":"Unknown"},"3":{"description ":"","person_name":"Sanjay Anandaram","email":"sanjayanandaram@gmail.com","exparience":null,"location":"Unknown"},"4":{"description ":"","person_name":"Katia Gaika","email":"katia.gaika@gmail.com","exparience":null,"location":"Unknown"},"5":{"description ":"","person_name":"Sidnei Gon\u008dalves","email":"sidnei.goncalves@up2place.com.br","exparience":null,"location":"Unknown"},"6":{"description ":"","person_name":"Michel Koch","email":"michelkoch@hotmail.com","exparience":null,"location":"Unknown"},"7":{"description ":"","person_name":"Shannon Lydell Carter, MBA Technolo","email":"Shannon@truegame.com","exparience":null,"location":"Unknown"},"8":{"description ":"","person_name":"Jacob Hagemann","email":"jhagemann@gmail.com","exparience":null,"location":"Unknown"},"9":{"description ":"","person_name":"Amaresh Ramaswamy","email":"amareshr@live.com","exparience":null,"location":"Unknown"}}

Why this error is happening and how to get ride of this.Thanks in Advance

  • What does your response look like? – EricSSH Jan 31 '16 at 7:42
  • 2
    Possible duplicate of JSON deserialize into wrapper class errors – EricSSH Jan 31 '16 at 7:46
  • {"s":1,"v":{"0":{"email":"jlt@staranchor.com","location":"Unknown","person_name":"Justin Townsley"}}} is this what you ask for – moin khan Jan 31 '16 at 7:47
  • than how can i solved it.I saw that post but not understand anyway – moin khan Jan 31 '16 at 7:49
  • That JSON response you sent fails in JSON lint, is that accurate? – EricSSH Jan 31 '16 at 7:51
1

So After discussion with moin. We found issue.

After debugging the issue. I found the reason, issue in end point url

for example strTag = Business activities then we need to replace space between Business and activities with %20.

After replacing this it worked like charm..

Issue related to end point..

String url = 'http://clozer.3spire.net/public/user/'+strTag+'/'+cities;

AFter changing this to

String url = 'http://clozer.3spire.net/public/user/'+strTag.replaceAll(' ', '%20')+'/'+cities;

it worked...


I Know if we used Business activities without %20 then it wil automatically changed the space to %20 but it didn't worked...

Noted:- We tried using EncodingUtil.urlEncode(strTag, 'UTF-8') but in url encoding space replace with + so that the reason it didn't worked.

1

After looking at this closer and running it my execute anon you are getting a bad json response..

01:42:33:604 USER_DEBUG [24]|DEBUG|res.getBody(){"0":{"description ":"none","person_name":"none","email":"none","exparience":"none,"location":"none"}}

Notice that

 "exparience":"none,"location"  

is not a valid json string and you won't be able to deserialize that, I recommend checking that out first..

Here is the code that I was running for it, fix the end point

  HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
string strTag = 'Businessactivities';
String url = 'http://clozer.3spire.net/public/user/'+EncodingUtil.urlEncode(strTag, 'UTF-8')+'/65291';
CitiesResult result = new CitiesResult();
    req.setEndpoint(url);
    req.setMethod('GET');

    //these parts of the POST you may want to customize
    req.setCompressed(false);
    req.setBody('{"s":1,"v":{"0":{"email":"jlt@staranchor.com","location":"Unknown","person_name"‌:"Justin Townsley"}}}');
    req.setHeader('Content-Type', 'application/json');  

    try {
        res = http.send(req);       
    } 
    catch(System.CalloutException e) 
    {
        system.debug('Callout error: '+ e);

    } 

String json = '{"0":{"description ":"","person_name":"Justin Townsley","email":"jlt@staranchor.com","exparience":"","location":"Unknown"},"1":{"description ":"","person_name":"Dan Anderson","email":"danandersonmobile@yahoo.com","exparience":"","location":"Unknown"},"2":{"description ":"","person_name":"Artak Robert Melkonyan","email":"artakrobert@gmail.com","exparience":"","location":"Unknown"},"3":{"description ":"","person_name":"Sanjay Anandaram","email":"sanjayanandaram@gmail.com","exparience":"","location":"Unknown"},"4":{"description ":"","person_name":"Katia Gaika","email":"katia.gaika@gmail.com","exparience":"","location":"Unknown"},"5":{"description ":"","person_name":"Sidnei Gon\u008dalves","email":"sidnei.goncalves@up2place.com.br","exparience":"","location":"Unknown"},"6":{"description ":"","person_name":"Michel Koch","email":"michelkoch@hotmail.com","exparience":"","location":"Unknown"},"7":{"description ":"","person_name":"Shannon Lydell Carter, MBA Technolo","email":"Shannon@truegame.com","exparience":"","location":"Unknown"},"8":{"description ":"","person_name":"Jacob Hagemann","email":"jhagemann@gmail.com","exparience":"","location":"Unknown"},"9":{"description ":"","person_name":"Amaresh Ramaswamy","email":"amareshr@live.com","exparience":"","location":"Unknown"}}';

System.debug('response code' + res.getStatusCode());
    System.debug('res.getBody()' + res.getBody());
    result = (CitiesResult)System.JSON.deserialize(json, CitiesResult.class);
System.debug('result' +result);

public class CitiesResult
{
    public CitiesResult(){}

    public String description {get;set;}
    public String person_name {get;set;}
    public String email {get;set;}
    public String exparience {get;set;}
    public String location {get;set;}
}

/* public class citiesResultWrapper{

 public Result objResult {get;set;}
 public Boolean isSelected {get;set;}

 public citiesResult(Result objResult){
   this.objResult = objResult;
   isSelected = false;
 }   
}*/

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.