[SalesForce] Looping through the parsed JSON string

I have used JSON2Apex to create a wrapper class from my JSON string. So far, all good.
My JSON string is somewhat like this:

String json = '{' +
    '\"MasterData\": {' +
    '\"RecordCount\": 21,' +
    '\"FormDataRecords\": {' +
        '\"FormData\": [' +
                '{' +
                    '\"FormID\": 930471394,' +
                    '\"FormDateTime\": \"2013-03-11T00:00:00.000+05:30\",' +
                    '\"Amount\": 330,' +
                    '\"Balance\": 0,' +
                    '\"Addons\": {' +
                        '\"Addon\": [' +
                                    '{' +
                                        '\"AddonType\": \"Quicker\",' +
                                        '\"AddonDesc\": \"This is a special Addon\"' +
                                    '}' +
                                ']' +
                        '}' +
                '},' +
                '{' +
                    '\"FormID\": 330471949,' +
                    '\"FormDateTime\": \"2013-03-11T00:00:00.000+05:30\",' +
                    '\"Amount\": 450,' +
                    '\"Balance\": 10,' +
                    '\"Addons\": {' +
                        '\"Addon\": [' +
                                    '{' +
                                        '\"AddonType\": \"Flash\",' +
                                        '\"AddonDesc\": \"This is a special Addon\"' +
                                    '}' +
                                ']' +
                        '}' +
                '}' +
            ']' +
        '}' +
    '}' +
    '}' 

After successful parsing the above JSON string using my JSON2Apex class, I was able to loop through the the FormData list like this:

for(Integer i=0; i<obj.MasterData.FormDataRecords.FormData.size(); i++){
  System.debug('FormID of '+ i +' is: ' +obj.MasterData.FormDataRecords.FormData[i].FormID);
}

My question is, is the above way of looping through the list of records is a good practice? If not, can I use the standard loop: for(Object obj: objList) or any other good approach?

Update:

This is what I would get once I deserialize using my parser class:

[MasterData=MasterData:
[FormDataRecords=FormDataRecords:
   [FormData=
       (
         FormData:[Addons=Addons:[Addon=(Addon:[AddonDesc=This is a special Addon, AddonType=Quicker])], Amount=330, Balance=0, FormDateTime=2013-03-11T00:00:00.000+05:30, FormID=930471394], 
         FormData:[Addons=Addons:[Addon=(Addon:[AddonDesc=This is a special Addon, AddonType=Flash])], Amount=450, Balance=10, FormDateTime=2013-03-11T00:00:00.000+05:30, FormID=330471949]
      )
    ], 
RecordCount=21]]

Best Answer

It is a bit easier to read if you just iterate through directly, without index:

for (JSON2Apex.FormData data : obj.MasterData.FormDataRecords.FormData)
{
    String formId = data.FormId;
}
Related Topic