Skip to content

Instantly share code, notes, and snippets.

@emoran
Created June 23, 2015 03:37
Show Gist options
  • Save emoran/2f480ed17ae6da1fefb2 to your computer and use it in GitHub Desktop.
Save emoran/2f480ed17ae6da1fefb2 to your computer and use it in GitHub Desktop.
Deserialize JSON to the exact Wrapper class structure
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