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 

Json parser in salesforce

string jsonexample1 = ' { "states" : [ "india", "alaska", "china" ] } ';


how to parse the data in the picklist in the apex:repeat functionalities , 

Another scenario :

 if the data is 

 string jsonexample2 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "taxpercent": 15.0 }, { "stateName": "Telangana",  "taxpercent": 18.0 }, {"stateName": "Banglore",  "taxpercent": 16.0 } , {"stateName": "Maharastra",  "taxpercent": 14.0 }  ] } '; 


here i need to get the statename in the picklist and taxpercent in a  Repeat table .. can anybody guide me how to proceed thanks ... 






 
Best Answer chosen by Deepikareddy
Neha AggrawalNeha Aggrawal
Same way, just add activestatus and pincode in two different maps with statename as the key. Than visualforce page print that map value.

statemap.put(statename, rating);
activestatusmap.put(statename, activestatus);

and in visualforce page: 
<td>{!activestatusmap[a]}</td>

Your key remains same that is 'statename'. 
May be just read through the concept of maps in Apex to understand better.
Thanks.

All Answers

Neha AggrawalNeha Aggrawal
Hi Deepika,

I did like this:
 
public class etg{

public void test(){
         Map<String,Object> values = new Map<String, Object>();

string jsonexample2 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "taxpercent": 15.0 }, { "stateName": "Telangana",  "taxpercent": 18.0 }, {"stateName": "Banglore",  "taxpercent": 16.0 } , {"stateName": "Maharastra",  "taxpercent": 14.0 }  ] } ';
Map<String, Object> metadata_Map = (Map<String, Object>) JSON.deserializeUntyped(jsonexample2);
system.debug(metadata_Map);
   List<Object> values1 = (List<Object>) metadata_Map.get('overalldata');
   system.debug(values1);
    for(Object mpParsed : values1)
        {
             values = (Map<String,Object>)mpParsed;
            String statename=String.valueOf(values.get('stateName'));
            system.debug(statename);
            }
}
}

This is my result:

User-added image

Thanks.
I hope you find the above solution helpful.
Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)
 
Neha AggrawalNeha Aggrawal
Hi Deepika,

What error ar you getting. Please post the code here.
Thanks.
DeepikareddyDeepikareddy
<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>
 
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);
            
             }
       
 }
}


hi neha, iam getting the value, but it is gettng iterating again and again , if i am doing it iterable when iam using the  html table, , please find the screenshot attached .. but it should not get iterate..  can u please provide the solution for this .. thanks in advance


it should not be get itereable
 
Neha AggrawalNeha Aggrawal
Hi Deepika,

The reason this ia hapening because you are running two loops with two <apex:repeat>. The easier way to find this is simply to add a debug statement in your apex class, somehting like "System.debug(lststatename);". 
When that prints correctly you know, that issue is with the visualforce page. 

This is the updated code:
public class test5 {

  public list<string> lststatename{get;set;}
  public list<string> lstrating{get;set;}
  public map<string,String> statemap{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>();
         statemap= 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'));
               
             lststatename.add(statename );
             lstrating.add(rating);
             statemap.put(statename, rating);
            
             }
       
 }
}
<apex:page controller="test5" >

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

  <tr>
   
   <td>{!a}</td>
  
    
    <td>{!statemap[a]}</td>
  </tr>
   </apex:repeat>

</table>
    
 
 </apex:form>
 
</apex:page>

I have just made a map and printed that in a single loop.
I hope you find the above solution helpful.
Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://initaura.com)
DeepikareddyDeepikareddy
hi , neha  1st i would like to say thank you, the code you had given is executed sucessfully , thank you .. 


  if i need to add some other values to the repeat  tag then?

like : 
string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": 5.0 , "active": "yes" , "pincode": 01  }, { "stateName": "Telangana",  "rating": 4.0 ,"active": "no" , "pincode": 02  }, {"stateName": "Banglore",  "rating": 5.0 ,"active": "no" ,  "pincode": 03 } , {"stateName": "Maharastra",  "rating": 4.5 ,"active": "no"  , "pincode": 04  }  ] } ';




extra values like : activestatus and pincode, then how to proceed, any idea ,


it will be helpfull in future , such that not to post again for the same query..


thanks 
deepika.

 
Neha AggrawalNeha Aggrawal
Same way, just add activestatus and pincode in two different maps with statename as the key. Than visualforce page print that map value.

statemap.put(statename, rating);
activestatusmap.put(statename, activestatus);

and in visualforce page: 
<td>{!activestatusmap[a]}</td>

Your key remains same that is 'statename'. 
May be just read through the concept of maps in Apex to understand better.
Thanks.
This was selected as the best answer
DeepikareddyDeepikareddy
Hi, hope u are doing good, Thank u so much  neha., it was cleared, will go through the map concepts to understand better . 

thanks !
Deepika