+ Start a Discussion
DeepikareddyDeepikareddy 

using the apex:repeat functionality in salesforce

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);
            
             }
       
 }
}
 
<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>

User-added image
 here to use the html table to display the values, but the data is getting iterated again , Any body have any idea to handle this. thanks in advance .. 
Best Answer chosen by Deepikareddy
Raghu NaniRaghu Nani
hi its working for below code..
vf page replace table with below code

<table>
  <tr>
    <th>states</th>
    <th>rating</th>
  </tr>
  <apex:repeat value="{!yourMap}" var="key">
    <tr>
        <td>{!key}</td><!-- key --->
        <td>{!yourMap[key]}</td> <!-- Value--->
    </tr>
  </apex:repeat>
</table>


and APEX CLASS , i added "yourMap" in apex
public class test5 {

    public map<string,string> yourMap {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');
        
        yourMap = new MAP<String,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'));
               yourMap.put(statename, rating);
            
             }
       
 }
}

All Answers

Raghu NaniRaghu Nani
Hi
Problem is you added repeat inside repeat in page. Please fix that one
Raghu NaniRaghu Nani
Instead of creating two list strings,
public list<string> lststatename{get;set;}
public list<string> lstrating{get;set;}

ypu can use simple map( MAP<String,decimal>) to store the values and display it
 
DeepikareddyDeepikareddy
Hi raghu, i had checked with that too, but it was showing an error .. can u please check with the code in ur org

thank u
Raghu NaniRaghu Nani
OK let me try
DeepikareddyDeepikareddy
oK thanks raghu...!
Raghu NaniRaghu Nani
hi its working for below code..
vf page replace table with below code

<table>
  <tr>
    <th>states</th>
    <th>rating</th>
  </tr>
  <apex:repeat value="{!yourMap}" var="key">
    <tr>
        <td>{!key}</td><!-- key --->
        <td>{!yourMap[key]}</td> <!-- Value--->
    </tr>
  </apex:repeat>
</table>


and APEX CLASS , i added "yourMap" in apex
public class test5 {

    public map<string,string> yourMap {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');
        
        yourMap = new MAP<String,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'));
               yourMap.put(statename, rating);
            
             }
       
 }
}
This was selected as the best answer