0

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]]
3

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;
}
  • Hey @Adrian, I have tried this and I get the error: Invalid Type: FormData. I know why I am seeing this, but how can I overcome it? – Austin Evans Aug 7 at 19:49
  • You have not showed us any of the structure you are deserializing into... – Adrian Larson Aug 7 at 19:51
  • Updated my question with it. – Austin Evans Aug 7 at 20:10
  • 1
    @AustinEvans Is FormData an inner class in some other class? You would need to use the fully qualified type name. – Adrian Larson Aug 7 at 20:36
  • That's great. yeah, I got it working once I had the full path in place. Can you please edit your answer with JSON2Apex.FormData, so that I can mark it as an answer? – Austin Evans Aug 7 at 20:59
1

To add to @AdrianLarson's answer, you should iterate directly. I have just generated the JSON2Apex class myself based on the above JSON string and I was able to loop through the FormData records in this way:

for(JSON2Apex.FormData data: obj.MasterData.FormDataRecords.FormData){
     System.debug('FormId: ' +data.FormId);
}

JSON2Apex class:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
 public class JSON2Apex {

 public class FormData {
    public Integer FormID;
    public String FormDateTime;
    public Integer Amount;
    public Integer Balance;
    public Addons Addons;
 }

 public class Addon {
    public String AddonType;
    public String AddonDesc;
 }

 public class MasterData {
    public Integer RecordCount;
    public FormDataRecords FormDataRecords;
 }

 public MasterData MasterData;

 public class Addons {
    public List<Addon> Addon;
 }

 public class FormDataRecords {
    public List<FormData> FormData;
 }


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.