This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '{"name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}}'; | |
| Account a = (Account) JSON.deserialize(input, Account.class); | |
| system.assertEquals(a.name, 'paulo'); | |
| system.debug(a); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class GetPerson { | |
| public String name {get;set;} | |
| public Integer age {get;set;} | |
| public Car car {get;set;} | |
| public class Car { | |
| public String model {get;set;} | |
| public String year {get;set;} | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '{"name":"paulo","age":42, "married":true, "car" : {"model" : "outlander", "year" : "2016"}}'; | |
| GetPerson person = (GetPerson) JSON.deserialize(input, GetPerson.class); | |
| system.assertEquals(person.name, 'paulo'); | |
| system.assertEquals(person.car.model, 'outlander'); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '[{"name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}}, {"name":"tin","age":40, "car" : {"model" : "crv", "year" : "2004"}}]'; | |
| List<GetPerson> person = (List<GetPerson>) JSON.deserialize(input, List<GetPerson>.class); | |
| system.assertEquals(person.size(), 2); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '{"name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}}'; | |
| Account a = (Account) JSON.deserializeStrict(input, Account.class); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | [ | |
| { | |
| "_id": "1", | |
| "name": "paulo", | |
| "age": 42, | |
| "car": { | |
| "model": "outlander", | |
| "year": "2016" | |
| }, | |
| "kids": [ | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class GetPerson { | |
| public String x_id {get;set;} | |
| public String name {get;set;} | |
| public Integer age {get;set;} | |
| public List<String> kids {get;set;} | |
| public Car car {get;set;} | |
| public class Car { | |
| public String model {get;set;} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '[{"_id":"1","name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}, "kids" : ["penny", "padma", "amber", "pauline"]}, {"_id":"2","name":"tin","age":40, "car" : {"model" : "crv", "year" : "2004"},"kids" : ["mary", "sophie", "patrice", "laeticia"]}]'; | |
| JSONParser parser = JSON.createParser(input); | |
| List<GetPerson> gpList = new List<GetPerson>(); | |
| while(parser.nextToken() != JSONToken.END_ARRAY) { // we started with an array of objects | |
| GetPerson gp = new GetPerson(); | |
| while(parser.nextToken() != JSONToken.END_OBJECT){ // loop through each object | |
| if(parser.getCurrentToken() == JSONToken.FIELD_NAME) { //token should be field name | |
| String attr = parser.getText(); //get the text of the field name | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class GetPerson { | |
| public String x_id {get;set;} | |
| public String name {get;set;} | |
| public Integer age {get;set;} | |
| public List<String> kids {get;set;} | |
| public Car car {get;set;} | |
| public class Car { | |
| public String model {get;set;} | |
| public String year {get;set;} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | String input = '[{"_id":"1","name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}, "kids" : ["penny", "padma", "amber", "pauline"]}, {"_id":"2","name":"tin","age":40, "car" : {"model" : "crv", "year" : "2004"},"kids" : ["mary", "sophie", "patrice", "laeticia"]}]'; | |
| List<GetPerson> gpList = GetPerson.parse(input); | |
| system.debug(gpList); |