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
Flight PlanFlight Plan 

How to get object from JSON parser ?????

Hi All,

I have a JSON response in this format ,


{"response":[{
        "field1" : "Developer",
        "field2":{"type":"Entery Level"},
        "field3":{"dynamicdata":{"Objectfield":{"Objectfield":"abc"}}}
        },
        {
        "field1" : "Developer",
        "field2":{"certification":"501 certified"},
        "field3":{"language":"Apex"}}
        },
        {
        "field1" : "Developer",
        "field2":{"hasBlog":"true"},
        "field3":{"Grade":{"A":{"marks":10}}}
        },
        {
        "field1" : "Developer",
        "field2":{"isContributor":"true"},
        "field3":{"Rank":{"A":{"marks":10}}}
        }
        ]
}


Using JSONParser parser = JSON.createParser(response.getBody());  to parse it.
I am able to get the proper values for field1. However, for fields "field2" & "field3" getting values like "{" as it is refering object.But parser class doesnt contain any method to get the Object.
As this fields hold JSON with no specified format.And I want to map this fields value to class field as a String like ApexClass.field3 = '{"Grade":{"A":{"marks":10}}}'.

In this scenario, I cannt use deserializeuntype as it only deals with primitive data types and I have a Apex class to map this JSON response.

The code looks like

JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
                if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                    while (parser.nextToken() != null ) {
                        if(parser.getCurrentToken() == JSONToken.START_OBJECT) {
                            while (parser.nextToken() != null){
                              if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
                                 String fieldName = parser.getText();
                                  parser.nextToken();
                                 String fieldvalue = parser.getText();
                                 System.debug('fieldName : '+fieldName + '    fieldValue  :'+fieldvalue);
                                 // After comparision assigning value to apex class fields and adding to a list.
                                
                               }
                            }
                        }
                      }
                   }
               }
 }

 
 If I am missing anything so please correct me .Also please suggest if there would be any better approach to handle this scenario.
 
 Thanks
 FP -

code developercode developer

Have you find any solution for this. I am facing same problem. Please post your answe if you have found a solution.

 

Thanks and Regards,

Surendranadh Nune