[SalesForce] Parser JSON array inside JSON array

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

{
  "name": [
    {
      "actions": "test",
      "Items": [
        {
            "Components": [
                {
                    "details": {
                        "label": "Name"
                    }
                }]
        },
        {
            "Components": [
                {
                    "details": {
                        "label": "Name"
                    }
                }]
        }]
    }]
}

public class JSON2Apex2 {

public class Components {
    public Details details;
}

public class Details {
    public String label;
}

public class JSON2Apex {
    public List<Name> name;
}

public class Items {
    public List<Components> Components;
}

public class Name {
    public String actions;
    public List<Items> Items{get;set;}
}


public static JSON2Apex parse(String json) {
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}

}

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)

JSON2Apex2.JSON2Apex obj = JSON2Apex2.parse(json);
for(JSON2Apex2.Name name : obj.Name){
system.debug('@@@@@' +name);
    for(JSON2Apex2.items item : name.items ){
        system.debug('#####'+item);
    }
}

Debug output:

11:30:38.0 (5020765)|USER_DEBUG|[24]|DEBUG|@@@@@Name:[Items=(Items:[Components=(Components:[details=Details:[label=Name]])], Items:[Components=(Components:[details=Details:[label=Name]])]), actions=test]

11:30:38.0 (5274035)|USER_DEBUG|[26]|DEBUG|#####Items:[Components=(Components:[details=Details:[label=Name]])]

11:30:38.0 (5356318)|USER_DEBUG|[26]|DEBUG|#####Items:[Components=(Components:[details=Details:[label=Name]])]