+ Start a Discussion
Nani@SFDCNani@SFDC 

Deserialize bit of JSON Response

Hi,
Below is the JSON Response of External REST Service API. My requirement is to extract a piece of data from the response. I am unable to deserialize the piece of data which is in BOLD.

{"sports" :[{"name" :"tennis","id" :850,"uid" :"s:850","leagues" :[{"name" :"Association of Tennis Professionals","abbreviation" :"atp","id" :850,"groupId" :"atp","shortName" :"ATP","athletes" :[{"id" :760,"firstName" :"Jorge","lastName" :"Aguilar","fullName" :"Jorge Aguilar","displayName" :"Jorge Aguilar","shortName" :"J. Aguilar","links" :{"api" :{"athletes" :{"href" :"http://api.espn.com/v1/sports/tennis/atp/athletes/760"},"news" :{"href" :"http://api.espn.com/v1/sports/tennis/atp/athletes/760/news"},"notes" :{"href" :"http://api.espn.com/v1/sports/tennis/atp/athletes/760/news/notes"}},"web" :{"athletes" :{"href" :"http://espn.go.com/tennis/player/_/id/760?ex_cid=espnapi_public"}},"mobile" :{"athletes" :{"href" :"http://m.espn.go.com/general/tennis/playercard?playerId=760&ex_cid=espnapi_public"}}}}]
}]}],"resultsOffset" :0,"resultsLimit" :50,"resultsCount" :1,"timestamp" :"2014-02-19T07:35:25Z","status" :"success"}
Sunil SharmaSunil Sharma
Hi,

You can try like this ---

JSONParser parser = JSON.createParser(response.getBody());
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                 while (parser.nextToken() != null) {
                       if(parser.getCurrentToken() == JSONToken.START_OBJECT){
                           while (parser.nextToken() != null) {

                               if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'athletes') ){
                                  
                                   r = new subClass ();

                                   currObj = parser.getText();
                                   continue;
                               }
                               if( ( parser.getCurrentToken() == JSONToken.START_OBJECT ) && ( currObj =='athletes' ) ) {

                                   while (parser.nextToken() != null) {
                                       if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'id')){

                                           parser.nextValue();
                                           r.f = parser.getText();
                                           continue;
                                      
           
            }
            }
           }}}}}

Thanks
Sunil Sharma


Developer99Developer99
@Nani@SFDC

   Can yo post your code once how you are connecting to third party system..
Sunil SharmaSunil Sharma
   Hi,

     You can try like this for consuming REST based web service (connecting to third party system)---

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String url = 'end point url';
        string un = 'username';
        string pw = 'password';
        req.setEndpoint(url);
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        Blob headerValue = Blob.valueOf(un+ ':' + pw);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);           
        req.setHeader('Authorization', authorizationHeader);
        HttpResponse res = h.send(req);
        System.debug('BODY: ' + res.getBody());
        //resp is a JSON string
        JSONParser parser = JSON.createParser(res.getBody());


Thanks
Sunil Sharma
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi Nani,

Do you have a wrapper class for a properties mentioned in the JSON?

I not, create a wrapper class with the property of athletes should be a list of another wrapper class.  Because, atheletes is meaning  a list of values rgt?


Example:

For deserializing a JSON string,


public static String func(String activitiesJSON) 
{
List<WrapperClass1> ActivityID_List= (WrapperClass1) JSON.deserialize(activitiesJSON, WrapperClass1.class);
//Your logic here
}

//WrapperClass1
public WrapperClass1{
    //properties
}




Regards,
Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator

Developer99Developer99
@Kamatchi Devi

  I want to fetch the records of Accounts,contacts ,opportunities from third party system and i want to save the records into my objects where i want to declare my parameters can you give me sample program for this.PlZ