Parsing the JSON object in salesforce

 
For example- Its a JSON output. We have to parse this.
{
"CompanyContacts": [
{
"contact": "pn0",
"postalcode": "0",
"contactnumber": "pc0"
},
{
"contact": "pn1",
"postalcode": "1",
"contactnumber": "pc1"
},
{
"contact": "pn2",
"postalcode": "2",
"contactnumber": "pc2"
}
]
}

 
public class CompanyContacts
{
public List CompanyContacts;
}

public class CompanyContactsWrapper
{
public String contact;
public String postalcode;
public String contactnumber;
}

then deserialize your JSON string in a new instace of the classes you created.

 
CompanyContacts.companycontacts = (CompanyContacts)JSON.deserialize(jsonstring, CompanyContacts.class);

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply