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 

Deserialing the array data in the apex repeat tag

public class test5 {

  public list<string> lststatename{get;set;}
  public list<string> lstrating{get;set;}

 public test5(){
 
 
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
     map<string,object>  metadatamap= (map<string,object>)json.deserializeuntyped(jsonexample1); 
     
      list<object>  values1= (list<object>)metadatamap.get('overalldata');
        
        
         lststatename= new list<string>();
         lstrating= new list<string>();
         
          for(object parsed : values1){
             
             
           map<string,object>  values = (map<string,object>)parsed;
             
             
            string statename = string.valueof(values.get('stateName'));
             string rating= string.valueof(values.get('rating'));
               
             lststatename.add(statename );
             lstrating.add(rating);
            
             }
       
 }
}
visualforce page:


 
<apex:page controller="test5" >

 <apex:form >
 
 <table>
  <tr>
    <th>states</th>
    <th>rating</th>
  </tr>
  
  <apex:repeat value="{!lststatename}" var="a">
  <apex:repeat value="{!lstrating}" var="b">
  <tr>
   
   <td>{!a}</td>
  
    
    <td>{!b}</td>
  </tr>
   </apex:repeat>
   </apex:repeat>
</table>
    
 
 </apex:form>
 
</apex:page>

hi ,  im getting the data  in the visualforce page , but the data is getting iterable when i use the repeat functionality inside the table , can anybody have the solution for this  ??it should not iterate again .. thanks in advance


 the data should not be get iterate again,
 
Best Answer chosen by Deepikareddy
Sunil RathoreSunil Rathore
Hi Deepika,

One more update needs to be done. Please refer the below final updated code:
public class test5 {
   public List<Overalldata> overalldata{get;set;}

  public test5(){	
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
    overalldata = (List<Overalldata>)json.deserializeuntyped(jsonexample1);     
      
    }
    //Wrapper class
	public class Overalldata {
		public String stateName;
		public Double rating;
	}
}

Also, find the attached image of the class in my org.
User-added image

Hope this will solve your problem.  If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
 

All Answers

Sunil RathoreSunil Rathore
Hi Deepika,

Greetings to you!

Please refer the below code:
Apex Class
public class test5 {

 public test5(){
 
	public List<Overalldata> overalldata;
	
	//Wrapper class
	public class Overalldata {
		public String stateName;
		public Double rating;
	}
 
 
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
    overalldata = (Overalldata)json.deserializeuntyped(jsonexample1);     
      
    }
}

VF page
<apex:page controller="test5" >
<apex:pageblock>

	 <apex:pageBlockTable value="{!overalldata}" var="item">

            <apex:column value="{!item.stateName}"/>
			<apex:column value="{!item.rating}"/>
	</apex:pageBlockTable>
</apex:pageblock>

</apex:page>

Hope this will help you. If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
DeepikareddyDeepikareddy
Hi sunil, thanks for ur reply, can u please do it in the  html table,  with other functionality like repeat or datatable
thanks
Sunil RathoreSunil Rathore
Hi Deepika,

Please refer the below code using HTML table:
<apex:page controller="test5" >
<apex:pageblock>

<table>
  <tr>
    <th>states</th>
    <th>rating</th>
  </tr>
  
  <apex:repeat value="{!Overalldata}" var="data">
  <tr>
	<td>{!data.stateName}</td>
    <td>{!data.rating}</td>
  </tr>
  </apex:repeat>
  </table>

</apex:page>

Hope this will help you. If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
DeepikareddyDeepikareddy
User-added image


Hi sunil, iam getting the below error, may i know the reason behind it.. 
Sunil RathoreSunil Rathore
Hi Deepika,

Remove the wrapper class from the constructor. Refer the below code
public class test5 {

 public test5(){
 
	public List<Overalldata> overalldata;
	
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
    overalldata = (Overalldata)json.deserializeuntyped(jsonexample1);     
      
    }
    //Wrapper class
	public class Overalldata {
		public String stateName;
		public Double rating;
	}
}

Hope this will help you. If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
DeepikareddyDeepikareddy

hi sunil sorry ,  removed ,the  copied the same code in the apex class  of name test6 ,but it is not getting save , it was showing the below error ,any   prerequisite is there , to done before saving the class? thank u
User-added image
Sunil RathoreSunil Rathore
Hi Deepika.

No there is no prerequisite to save this class. Make sure you also use the same class name for the controller of VF page.
Please use the below code:
public class test5 {
   public List<Overalldata> overalldata{get;set;}

  public test5(){	
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
    overalldata = (Overalldata)json.deserializeuntyped(jsonexample1);     
      
    }
    //Wrapper class
	public class Overalldata {
		public String stateName;
		public Double rating;
	}
}

 
DeepikareddyDeepikareddy
Hi , sunil, can u please , paste it in your developement org, and share the screenshot, it will be helpful to me.. 

thanks in advance
Sunil RathoreSunil Rathore
Hi Deepika,

One more update needs to be done. Please refer the below final updated code:
public class test5 {
   public List<Overalldata> overalldata{get;set;}

  public test5(){	
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 }, { "stateName": "Telangana",  "rating": 4.0 }, {"stateName": "Banglore",  "rating": 5.0 } , {"stateName": "Maharastra",  "rating": 4.5 }  ] } ';

   
    overalldata = (List<Overalldata>)json.deserializeuntyped(jsonexample1);     
      
    }
    //Wrapper class
	public class Overalldata {
		public String stateName;
		public Double rating;
	}
}

Also, find the attached image of the class in my org.
User-added image

Hope this will solve your problem.  If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
 
This was selected as the best answer
DeepikareddyDeepikareddy
Hi sunil, it was saved sucesfully, when iam using the visualforce page in the apex class, it is showing an error, can u please  help it out with visualforce page and execute the above format , thank you.. in advance ..