I am facing issue with parsing jsonArray below
{
"name": [
{
"actions": "test",
"Items": [
{
"Components": [
{
"details": {
"label": "Name"
}
}]
},...
{
"Components": [
{
"details": {
"label": "Name"
}
}]
}]
}]
}
I have written a wrapper class as well as below
public class Name{
public String actions{get; set;}
public Item items{get; set;}
public class Item{
public Component components {get; set;}
}
public class Component{
public detail details {get; set;}
}
public class detail{
public String label {get; set;}
}
public static Name parse(String json){
return (Name) System.JSON.deserialize(json,
Name.class);
}
}
whenever I am trying to do
System.debug(name.items[0].components[0].details.label);
I am getting the label value but what should i do if there are many values in "Items", "componemts"?
Do I have to iterate over all the arrays?
Is there any way without using looping 2 times?
Best Answer
1) The wrapper class does not look right. I used JSON2APEX to generate the class using the JSON from your question(excluded the ...) and this is what it comes out to
public class JSON2Apex2 {
}
2) Can you do it without 2 arrays, you cannot you have name array inside the name array you have items array inside the items array you have components array
so you have to use( I named my class from JSON2APEX as JSON2APEX2, so name your class accordingly)
Debug output: