function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya manohardivya manohar 

unable to deserialize

Hello
This is my first post.

I have a json string which I want to deserialize and get the values.

public class CompanyContactsWrapper
{
     public string contact{get;set;}
     public string postalcode{get;set;}
     public string contactnumber{get;set;}
}

public class Jsonexample4
{
   public CompanyContactsWrapper CompanyContacts{get;set;}
   
   public Jsonexample4()
   {
    
     string jsonInput='{"CompanyContacts":[{"contact":"pn0","postalcode":"0","contactnumber":"pc0"},{"contact":"pn1","postalcode":"1","contactnumber":"pc1"},{"contact":"pn2","postalcode":"2","contactnumber":"pc2"}]}';
          
          
     CompanyContacts= (CompanyContactsWrapper)JSON.deserialize(jsonInput,CompanyContactsWrapper.class);
         
     system.debug('==>'+CompanyContacts);
   
   }
}

In developer console I create an object and execute.
I get contact=null, postalcode=null & contactnumber=null.

pls tell where I have gone wrong?

thanks
divya manohar
Best Answer chosen by divya manohar
Himanshu ParasharHimanshu Parashar
Update your code in following way

 
public with sharing class CompanyContactsWrapper {
    
    public cls_CompanyContacts[] CompanyContacts;
    class cls_CompanyContacts {
        public string contact{get;set;}
     public string postalcode{get;set;}
     public string contactnumber{get;set;}
    }

}

Thanks,
Himanshu
 

All Answers

Himanshu ParasharHimanshu Parashar
Update your code in following way

 
public with sharing class CompanyContactsWrapper {
    
    public cls_CompanyContacts[] CompanyContacts;
    class cls_CompanyContacts {
        public string contact{get;set;}
     public string postalcode{get;set;}
     public string contactnumber{get;set;}
    }

}

Thanks,
Himanshu
 
This was selected as the best answer
divya manohardivya manohar
Hi 
thanks. it worked.