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
meghna nmeghna n 

read json array values

I have json arrya as follows inside lightning component controller

 var allResultsArray=JSON.stringify(component.get("v.allResults"));

[{"Name":"Cardiology","Code":"839"},{"Name":"Pediatric Cardiology","Code":"824"},{"Name":"Cardiothoracic Surgery","Code":"701"},{"Name":"Interventional Cardiology","Code":"726"},{"Name":"Pediatric Cardiothoracic Surgery","Code":"707"}]

var isName,isCode;

I want to read the Name and Code in variables

I am doing as follows

for(var i=0;i<allResultsArray.length;i++)
{
  isName =allResultsArray[i].Name;
alert(isName);
}

But I am not able to get the vauies of Name  and Code from the above json array

please let me know how I can achieve this.

thanks
Meghna

 
Team NubesEliteTeam NubesElite
Hi Meghna,
Please try below code it will work for your requirement
public TestClass(){
    string  jsonexample1 =  ' { "overalldata": [ {"Name":"Cardiology","Code":"839"},{"Name":"Pediatric Cardiology","Code":"824"},{"Name":"Cardiothoracic Surgery","Code":"701"},{"Name":"Interventional Cardiology","Code":"726"},{"Name":"Pediatric Cardiothoracic Surgery","Code":"707"}]}';
     map<string,object>  metadatamap= (map<string,object>)json.deserializeuntyped(jsonexample1); 
     list<object>  values1= (list<object>)metadatamap.get('overalldata');
     lstname= new list<string>();
     lstcode= new list<string>();
     InfoLst = new List<InfoLst>();
     for(object parsed : values1){ 
     map<string,object>  values = (map<string,object>)parsed;
     string name = string.valueof(values.get('Name'));
     string code = string.valueof(values.get('Code'));
     lstname.add(name );
     lstcode.add(code);     
     InfoLst.add(new Info(name,code));       
     }
Thank You
www.nubeselite.com

Developement | Training | Consulting

Please mark this as solution if your problem resolved.
 
meghna nmeghna n
Hi Team NubesElite

The JSON I shown does not contain "overalldata", You have added it to my JSON and shown code but I cannot do the same in my lightning component.

can u pls show again without using " overalldata"?

thanks
Meghna