+ Start a Discussion
DeepikareddyDeepikareddy 

selectlist in salesforce

Hi i need to deserialize the json data into the selectlist , can any one please solve this issue..please .. 

Apex Class is :
public class test10 {

     public List<SelectOption> options {get;set;}

 public  test10(){
 
  check();
  }
  
  //wrapperclass1
   public class ContactWrapper {
   
        public string stateName{ get; set; }
        public string rating{get;set;}
        }
        
        
      public class overalldata
    {
        public List<ContactWrapper> overalldata;
    }  
  
    public void check(){
    
       overalldata conList = new overalldata();
    
     string  jsonstring='{ "sucess":1, "overalldata": [ {"stateName": "Andrapradesh",  "rating": "5.0" , "active": "yes" }, { "stateName": "Telangana",  "rating": "4.0" ,"active": "no" }, {"stateName": "Banglore",  "rating": "5.0" ,"active": "no"} , {"stateName": "Maharastra",  "rating": "4.5" ,"active": "no" }  ] } ';
     
     conList = (overalldata)System.JSON.deserialize(jsonstring, overalldata.class);
     
      System.debug('Respone- ' + conList);
     
    //getting data as this 
   //10:25:35:007 USER_DEBUG [31]|DEBUG|Respone- overalldata:[overalldata=(ContactWrapper:[rating=5.0, stateName=Andrapradesh], ContactWrapper:[rating=4.0, stateName=Telangana], ContactWrapper:[rating=5.0, stateName=Banglore], ContactWrapper:[rating=4.5, stateName=Maharastra])]
   
    }

  }

Visualforce page is:
 
<apex:page controller="test10">
 
  <apex:form >
        <apex:selectList >
            <apex:selectOptions value="{!options}"/>
            
             
        </apex:selectList>
        
      
       
    </apex:form>
 
 
</apex:page>

in the above selecoptions, the itemlabel  wil be stateName and itemvalue is rating.. please help to solve this issue..

thanks and regards

deepika

 
Best Answer chosen by Deepikareddy
Sonali_takkeSonali_takke
Hi deepika,

Please try below code.
 
public class test10 {
    public string selectedValue {get;set;}
    
    //wrapperclass1
    public class ContactWrapper {
        public string stateName{ get; set; }
        public string rating{get;set;}
    }
    
    public class overalldata {
        public List<ContactWrapper> overalldata;
    }  
    public List<SelectOption> getOptions(){
        List<ContactWrapper> contactData;
        List<SelectOption> options = new List<SelectOption>();
        overalldata conList = new overalldata();
        string  jsonstring='{ "sucess":1, "overalldata": [ {"stateName": "Andrapradesh",  "rating": "5.0" , "active": "yes" }, { "stateName": "Telangana",  "rating": "4.0" ,"active": "no" }, {"stateName": "Banglore",  "rating": "5.0" ,"active": "no"} , {"stateName": "Maharastra",  "rating": "4.5" ,"active": "no" }  ] } ';
        conList = (overalldata)System.JSON.deserialize(jsonstring, overalldata.class);
        System.debug('Respone- ' + conList);
        //getting data as this 
        //10:25:35:007 USER_DEBUG [31]|DEBUG|Respone- overalldata:[overalldata=(ContactWrapper:[rating=5.0, stateName=Andrapradesh], ContactWrapper:[rating=4.0, stateName=Telangana], ContactWrapper:[rating=5.0, stateName=Banglore], ContactWrapper:[rating=4.5, stateName=Maharastra])]
        contactData = conList.overalldata;
        System.debug('contactData- ' + contactData);
        for(ContactWrapper con: contactData){
            options.add(new SelectOption(con.rating,con.stateName));
        }
        return options;
    }
    public String getselectedValue() {
        return selectedValue;
    }
    
    public void setselectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }
}
 
<apex:page controller="test10">
	<apex:form >
    	<apex:selectList  value="{!selectedValue}"  size="1" multiselect="false">
            <apex:selectOptions value="{!options}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
Mark as resolved if you find this answer helpful..

All Answers

Sonali_takkeSonali_takke
Hi deepika,

Please try below code.
 
public class test10 {
    public string selectedValue {get;set;}
    
    //wrapperclass1
    public class ContactWrapper {
        public string stateName{ get; set; }
        public string rating{get;set;}
    }
    
    public class overalldata {
        public List<ContactWrapper> overalldata;
    }  
    public List<SelectOption> getOptions(){
        List<ContactWrapper> contactData;
        List<SelectOption> options = new List<SelectOption>();
        overalldata conList = new overalldata();
        string  jsonstring='{ "sucess":1, "overalldata": [ {"stateName": "Andrapradesh",  "rating": "5.0" , "active": "yes" }, { "stateName": "Telangana",  "rating": "4.0" ,"active": "no" }, {"stateName": "Banglore",  "rating": "5.0" ,"active": "no"} , {"stateName": "Maharastra",  "rating": "4.5" ,"active": "no" }  ] } ';
        conList = (overalldata)System.JSON.deserialize(jsonstring, overalldata.class);
        System.debug('Respone- ' + conList);
        //getting data as this 
        //10:25:35:007 USER_DEBUG [31]|DEBUG|Respone- overalldata:[overalldata=(ContactWrapper:[rating=5.0, stateName=Andrapradesh], ContactWrapper:[rating=4.0, stateName=Telangana], ContactWrapper:[rating=5.0, stateName=Banglore], ContactWrapper:[rating=4.5, stateName=Maharastra])]
        contactData = conList.overalldata;
        System.debug('contactData- ' + contactData);
        for(ContactWrapper con: contactData){
            options.add(new SelectOption(con.rating,con.stateName));
        }
        return options;
    }
    public String getselectedValue() {
        return selectedValue;
    }
    
    public void setselectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }
}
 
<apex:page controller="test10">
	<apex:form >
    	<apex:selectList  value="{!selectedValue}"  size="1" multiselect="false">
            <apex:selectOptions value="{!options}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
Mark as resolved if you find this answer helpful..
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Deepika,

Greetings to you!

Use the below links through which you may get help:

https://salesforce.stackexchange.com/questions/186063/display-json-data-in-inputfield-on-dropdown-change
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com