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
DeepikareddyDeepikareddy 

how to bind the values to the wrapper class in salesforce

public class jsontest {

  public  loandetails loandetails{get;set;}
  
 public jsontest (){

 string teststring ='{"status":1,"data":[{"$id":"1","type":"A","InterestAmount":"2.90","TotalAmount":"198000.00","year":2018,"Name":"Axis BANK"},{"$id":"2","type":"B","InterestAmount":"3.90","TotalAmount":"158000.00","year":2019,"Name":"ICICI BANK"},{"$id":"3","type":"A","InterestAmount":"2.90","TotalAmount":"258000.00","year":2018,"Name":"HDFC BANK"}]}';
 
           Map<String, Object> mapobj= (Map<String, Object>)JSON.deserializeUntyped(teststring);
           system.debug('the Mapped and deserialized values are:'+ mapobj);
           
            list<Object> objj = (list<Object>)mapobj.get('data');
            system.debug('the data value is :::'+objj);
           
            string SerilizeEmpdata = system.json.serialize(objj);
           system.debug('After Serilization:::'+ SerilizeEmpdata);
           
            loandetails =(loandetails)system.json.deserialize(SerilizeEmpdata,loandetails.class);
             system.debug(loandetails);
             system.debug('binded to loan information is:::'+loandetails.name);     
 
 }
 
  //wrapperclass
 
  public class loandetails{
  
    public string type{get;set;}
    public string InterestAmount{get;set;}
    public string TotalAmount{get;set;}
    public integer year{get;set;}
    public string name{get;set;}
  } 
}

Getting an error: 
System.JSONException: Malformed JSON: Expected '{' at the beginning of object
Best Answer chosen by Deepikareddy
Deepak GerianiDeepak Geriani

Hello  Deepikareddy,

you have to iterate through each object when you are serializing it as the class loan details doesn't accept the list of the JSON string.

Here is the code same as you have used i have just added for loop.

public class jsontest {

  public  loandetails loandetails{get;set;}
  
 public jsontest (){

 string teststring ='{"status":1,"data":[{"$id":"1","type":"A","InterestAmount":"2.90","TotalAmount":"198000.00","year":2018,"Name":"Axis BANK"},{"$id":"2","type":"B","InterestAmount":"3.90","TotalAmount":"158000.00","year":2019,"Name":"ICICI BANK"},{"$id":"3","type":"A","InterestAmount":"2.90","TotalAmount":"258000.00","year":2018,"Name":"HDFC BANK"}]}';
 
           Map<String, Object> mapobj= (Map<String, Object>)JSON.deserializeUntyped(teststring);
           system.debug('the Mapped and deserialized values are:'+ mapobj);
           
            list<Object> objj = (list<Object>)mapobj.get('data');
            system.debug('the data value is :::'+objj);
           
     for(Object obj : objj){
         string SerilizeEmpdata = system.json.serialize(obj);
         system.debug('After Serilization:::'+ SerilizeEmpdata);
         loandetails =(loandetails)system.json.deserialize(SerilizeEmpdata,loandetails.class);
         system.debug(loandetails);
         system.debug('binded to loan information is:::'+loandetails.name); 
     }
         
 }
    
  //wrapperclass
 
  public class loandetails{
  
    public string type{get;set;}
    public string InterestAmount{get;set;}
    public string TotalAmount{get;set;}
    public integer year{get;set;}
    public string Name{get;set;}
  } 
}


you can find the solution 

if it worked for you please mark as solved 

thanks

All Answers

Deepak GerianiDeepak Geriani

Hello  Deepikareddy,

you have to iterate through each object when you are serializing it as the class loan details doesn't accept the list of the JSON string.

Here is the code same as you have used i have just added for loop.

public class jsontest {

  public  loandetails loandetails{get;set;}
  
 public jsontest (){

 string teststring ='{"status":1,"data":[{"$id":"1","type":"A","InterestAmount":"2.90","TotalAmount":"198000.00","year":2018,"Name":"Axis BANK"},{"$id":"2","type":"B","InterestAmount":"3.90","TotalAmount":"158000.00","year":2019,"Name":"ICICI BANK"},{"$id":"3","type":"A","InterestAmount":"2.90","TotalAmount":"258000.00","year":2018,"Name":"HDFC BANK"}]}';
 
           Map<String, Object> mapobj= (Map<String, Object>)JSON.deserializeUntyped(teststring);
           system.debug('the Mapped and deserialized values are:'+ mapobj);
           
            list<Object> objj = (list<Object>)mapobj.get('data');
            system.debug('the data value is :::'+objj);
           
     for(Object obj : objj){
         string SerilizeEmpdata = system.json.serialize(obj);
         system.debug('After Serilization:::'+ SerilizeEmpdata);
         loandetails =(loandetails)system.json.deserialize(SerilizeEmpdata,loandetails.class);
         system.debug(loandetails);
         system.debug('binded to loan information is:::'+loandetails.name); 
     }
         
 }
    
  //wrapperclass
 
  public class loandetails{
  
    public string type{get;set;}
    public string InterestAmount{get;set;}
    public string TotalAmount{get;set;}
    public integer year{get;set;}
    public string Name{get;set;}
  } 
}


you can find the solution 

if it worked for you please mark as solved 

thanks

This was selected as the best answer
Soyab HussainSoyab Hussain
Hi Deepika,

You can use this code this will help you.
 
public class jsontest {  
    public  parentClass pClass {get;set;} 
    public jsontest (){ 
        string teststring ='{"status":1,"data":[{"$id":"1","type":"A","InterestAmount":"2.90","TotalAmount":"198000.00","year":2018,"Name":"Axis BANK"},{"$id":"2","type":"B","InterestAmount":"3.90","TotalAmount":"158000.00","year":2019,"Name":"ICICI BANK"},{"$id":"3","type":"A","InterestAmount":"2.90","TotalAmount":"258000.00","year":2018,"Name":"HDFC BANK"}]}';
        pClass =  (parentClass) JSON.deserialize(teststring, parentClass.class); 
        System.debug('pClass' + pClass);
    }
    
    //parent class
    public class parentClass{
        public String status{get;set;}
        public LIst<loandetails> data {get;set;}
    }
    //wrapperclass
    public class loandetails{ 
        public string type{get;set;}
        public string InterestAmount{get;set;}
        public string TotalAmount{get;set;}
        public integer year{get;set;}
        public string name{get;set;}
    } 
}

Regards,
Soyab