Created
June 23, 2015 03:37
-
-
Save emoran/2f480ed17ae6da1fefb2 to your computer and use it in GitHub Desktop.
Deserialize JSON to the exact Wrapper class structure
This file contains 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 cl_pruebas { | |
public cl_pruebas() { | |
} | |
public void init(){ | |
String my_json='{"account":"01ti0000006s5I4AAI","products":[{"id":"01ti0000006s5I8AAI","qty":"8","price":"1.00","name":"cebolla cambray c/rabano"},{"id":"01ti0000006s5I4AAI","qty":"10","price":"1.00","name":"carambola"}]}'; | |
//Deserialize from the original type, in this case Object | |
Object orden_compra = JSON.deserialize(my_json,main.class); | |
//Casting to the wrapper. NOTE: the name of the values needs to be the same | |
main principal = (main)orden_compra; | |
//Access to the accountId | |
System.debug('###'+principal.account); | |
//Loop over products inside the main object | |
for(producto p:principal.products){ | |
//printing the name and access to properties. | |
System.debug('Nombre: '+p.name); | |
} | |
} | |
public class main{ | |
public String account{get;set;} | |
List<producto> products{get;set;} | |
} | |
public class producto{ | |
public String id{get;set;} | |
public Integer qty{get;set;} | |
public Decimal price{get;set;} | |
public String name{get;set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment