0

Scenario : Deserialize the JSON format data using wrapper class. I have JSON data and wrapper class for the JSON. I want to see value for one of the property in inner class of the wrapper class. But I cannot able to access the inner class in my batch class. Below are the sample wrapper class and batch class

Wrapper class :

Public class JSON2ApexWrapperClass{

    public class Aliases
    {
        public string href { get; set; }
    }

    public class Comments
    {
        public string countat { get; set; }
        public string href { get; set; }
    }
     public static JSON2ApexWrapperClass parse(String json) {
        return (JSON2ApexWrapperClass) System.JSON.deserialize(json, JSON2ApexWrapperClass.class);
    }
}

Batch Class :

global void execute(Database.BatchableContext BC, List<Account> scope){
    string jsonResult = 'body of json';
    JSON2ApexWrapperClass jsonDeserialize = JSON2ApexWrapperClass.parse(jsonResult);
    system.debug('*******  Deserialized  *********'+jsonDeserialize);
    system.debug('*******  Deserialized  *********'+jsonDeserialize.Comments); //I CANNOT ABLE TO ACCESS COMMENTS CLASS

}

sample JSON string :

{
   "@count":"1",
   "@querycount":"1",
   "@ranking":"adaptive",
   "mindTouchPage":{
      "id":"333",
      "guid":"01955249d38a4c8681940f8e84b55b90",
      "draftstate":"inactive",
      "href":"https://sampleTest.us/@api/poki/pages/333?redirects=0",
      "deleted":"false",
      "@unpublish":"true",
      "@revision":"2",
      "@score":"1",
      "aliases":{
         "href":"https://sampleTest.us/@api/poki/pages/333/aliases"
      },
      "comments":{
         "@count":"0",
         "href":"https://sampleTest.us/@api/poki/pages/333/comments"
      }  
   }
}

How to access inner class in batch class and how to see the deserialized values of comments class.

1
  • 1
    Can you please post a sample JSON?
    – Saroj Bera
    Jun 21, 2016 at 9:52

1 Answer 1

1

Access inner class

So if you want to access inner class variable then try something

JSON2ApexWrapperClass.Aliases objAliases = new JSON2ApexWrapperClass.Aliases();
system.debug(objAliases.href);//get href value

JSON2ApexWrapperClass.Comments objComments = new JSON2ApexWrapperClass.Comments();
system.debug(objComments.countat);//get href value
system.debug(objComments.href);//get href value

Updates

As per your JSON structure you can't create use apex class for deserialize the JSON because json code contains the @ in variable name

So you need to go with JSON.deserializeUntyped()

String myvar = '{'+
'   "@count":"1",'+
'   "@querycount":"1",'+
'   "@ranking":"adaptive",'+
'   "mindTouchPage":{'+
'      "id":"333",'+
'      "guid":"01955249d38a4c8681940f8e84b55b90",'+
'      "draftstate":"inactive",'+
'      "href":"https://sampleTest.us/@api/poki/pages/333?redirects=0",'+
'      "deleted":"false",'+
'      "@unpublish":"true",'+
'      "@revision":"2",'+
'      "@score":"1",'+
'      "aliases":{'+
'         "href":"https://sampleTest.us/@api/poki/pages/333/aliases"'+
'      },'+
'      "comments":{'+
'         "@count":"0",'+
'         "href":"https://sampleTest.us/@api/poki/pages/333/comments"'+
'      }  '+
'   }'+
'}';

map<String, object> mapJSON = (map<String, object>)JSON.deserializeUntyped(myvar);

map<String, object> mapJSON1 = (map<String, object>)mapJSON.get('mindTouchPage');

map<String, object>  mapAliases = (map<String, object>)mapJSON1.get('aliases');
map<String, object>  mapComments = (map<String, object>)mapJSON1.get('comments');

 //get the alias href value
system.debug(mapAliases.get('href'));
//get the comment @count value
system.debug(mapComments.get('@count'));
 //get the comment href value
system.debug(mapComments.get('href'));
10
  • Thanks for the help. I have deserialized json format like above but when am seeing in debug the value is coming as null. Any workaround needs to get the value?
    – sfdcDev
    Jun 21, 2016 at 10:07
  • @sfdcDev if you are not getting the parsing error then may be you are getting null value from your jason iteself
    – Ratan Paul
    Jun 21, 2016 at 10:12
  • there is value existing for href, countat in JSON but it is comingas null when deserializing
    – sfdcDev
    Jun 21, 2016 at 10:19
  • @sfdcDev can you pls share your JSON (pls add in your question) ?
    – Ratan Paul
    Jun 21, 2016 at 10:20
  • updated question with json
    – sfdcDev
    Jun 21, 2016 at 10:24

You must log in to answer this question.

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