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 

parse json to the selectlist

    string  jsonexample1 =  ' { "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" }  ] } ';


how  to parse  the data and assign to Selectlist , itemlabel will be statename and itemvalue will be  rating, based on selection i should display the rating value .. can any one help out thank u ... 

thanks 
deepika
Best Answer chosen by Deepikareddy
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="JsonInPicklistC">
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!JsonWrapperList}" var="val">
                {!val.stateName}<br/>
            </apex:repeat>
            <br/>
            <apex:selectList size="1"  value="{!selectedFields}">
                PickList : &nbsp; <apex:selectOptions value="{!items}"/>
                <apex:actionSupport event="onchange" reRender="one" />
            </apex:selectList>
            <br/>
            <apex:outputLabel id="one" >Rating: {!selectedFields}</apex:outputLabel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class JsonInPicklistC {
    
    public List<WrapperClass> JsonWrapperList{get;set;}
    public List<SelectOption> items{get;set;}
    public String selectedFields{get;set;}
    
    public JsonInPicklistC () {
        showJson();
        items = new List<SelectOption>();
        items.add(new SelectOption('--None--','--None--'));
        for(WrapperClass crRec : JsonWrapperList){
            items.add(new SelectOption(crRec.rating, crRec.stateName ));
        }   
    }
    
    public List<WrapperClass> showJson(){ 
        String jsonData = '[{"stateName":"Andrapradesh","rating":5.0,"active":"yes"},{"stateName":"Telangana","rating":4.0,"active":"no"},{"stateName":"Banglore","rating":3.5,"active":"no"},{"stateName":"Maharastra","rating":4.5,"active":"no"}]'; 
        JsonWrapperList= (List<WrapperClass>) System.JSON.deserialize(jsonData,List<WrapperClass>.class);
        return JsonWrapperList; 
    }   
    
    public class WrapperClass {
        public string stateName {get;set;}
        public string rating{get;set;}
        public string active{get;set;}
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="JsonInPicklistC">
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!JsonWrapperList}" var="val">
                {!val.stateName}<br/>
            </apex:repeat>
            <br/>
            <apex:selectList size="1"  value="{!selectedFields}">
                PickList : &nbsp; <apex:selectOptions value="{!items}"/>
                <apex:actionSupport event="onchange" reRender="one" />
            </apex:selectList>
            <br/>
            <apex:outputLabel id="one" >Rating: {!selectedFields}</apex:outputLabel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class JsonInPicklistC {
    
    public List<WrapperClass> JsonWrapperList{get;set;}
    public List<SelectOption> items{get;set;}
    public String selectedFields{get;set;}
    
    public JsonInPicklistC () {
        showJson();
        items = new List<SelectOption>();
        items.add(new SelectOption('--None--','--None--'));
        for(WrapperClass crRec : JsonWrapperList){
            items.add(new SelectOption(crRec.rating, crRec.stateName ));
        }   
    }
    
    public List<WrapperClass> showJson(){ 
        String jsonData = '[{"stateName":"Andrapradesh","rating":5.0,"active":"yes"},{"stateName":"Telangana","rating":4.0,"active":"no"},{"stateName":"Banglore","rating":3.5,"active":"no"},{"stateName":"Maharastra","rating":4.5,"active":"no"}]'; 
        JsonWrapperList= (List<WrapperClass>) System.JSON.deserialize(jsonData,List<WrapperClass>.class);
        return JsonWrapperList; 
    }   
    
    public class WrapperClass {
        public string stateName {get;set;}
        public string rating{get;set;}
        public string active{get;set;}
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
DeepikareddyDeepikareddy
hi , khan ana, thanks for ur reply..

 the json string format coming is:

    string  jsonData=  ' { "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" }  ] } ';

it is showing an Exception for me thank u..please let me know how to proced thank u

Deepika 
 
DeepikareddyDeepikareddy
Hi , from above format i had used two inner classes, and deserilized the data , 
 
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);
     
    //List<ContactWrapper> result = (List<ContactWrapper>)JSON.deserialize(conList, LIST<ContactWrapper>.class);

   
   
    }

  }

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

AFter debugging: iam getting the result as :

USER_DEBUG [29]|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])] 

Can u please help to proceed to next step to add in the selectoption. please . .thank you 

thanks and regards
deepika