You need to sign in to do that
Don't have an account?
Veena Gopal
difference in json2apex generated code
when using json2apex-http://json2apex.herokuapp.com
i got different codes for different json strings.
i got different codes for different json strings.
{"size":141,"totalSize":141,"done":true,"queryLocator":null,"entityTypeName":"ApexCodeCoverageAggregate","records":[{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715000000LxWDAA0"},"Id":"715000000LxWDAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01p000000C8J2AAK"},"Name":"MyCustomClass1"},"NumLinesCovered":0,"NumLinesUncovered":10},{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715e0000000LxWEAA0"},"Id":"710000000LxWEAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01pe0000000C8JCAA0"},"Name":"MyCustomClass2"},"NumLinesCovered":7,"NumLinesUncovered":5}]}for above json string i get
public static JSON2Apex parse(String json) { return new JSON2Apex(System.JSON.createParser(json));}for below json string
{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}i get
public static JSON2Apex parse(String json) { return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class); }please help me in understanding why is there so much difference in both the return statements which is in the end of parse code.
Method 1
Imagine we have a class like this - And we want to convert the json to an instance of the JSON2APEX class. We can use the deserialize method for this and this is the way to do this -
The deserialize method accepts two arguments. The first one is the json string and the second argument is the name of the class whose instance we want to create. We need to suffix ".class" with the class name. And finally we need to type cast this.
Method 2
The other method is, if we have any keywords invloved in our JSON then we have to use the create parser method of JSON class offered by Salesforce to generate a parse code for it. And you will have to manually parse every node of your JSON just as in your first example.
All Answers
Method 1
Imagine we have a class like this - And we want to convert the json to an instance of the JSON2APEX class. We can use the deserialize method for this and this is the way to do this -
The deserialize method accepts two arguments. The first one is the json string and the second argument is the name of the class whose instance we want to create. We need to suffix ".class" with the class name. And finally we need to type cast this.
Method 2
The other method is, if we have any keywords invloved in our JSON then we have to use the create parser method of JSON class offered by Salesforce to generate a parse code for it. And you will have to manually parse every node of your JSON just as in your first example.
In my JSON2Animal class when i used i am able to create a animal1 class and use the fields of the class by using the following inside AnimalLocator class. How will i get the name if i use below in json class and inside AnimalLocator class i will use below please tell me how will i take out name from this.