how to parse the JSON object in salesforce

Take a look at JSON.deserialize(); (documentation)

In your case, what you will have to do is create 2 inner classes like so:

public class CompanyContacts
{
     public List<CompanyContactsWrapper> 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);

now you'll be able to programatically approach the values of your json string in the new list.


The easiest solution (and most efficient in terms of script statements!) is going to be for you to head to Json2Apex (written by metadaddy and superfell), plug your JSON in, and away you go!

That will spit out this class:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class CompanyContactsParser {

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

public List<CompanyContacts> CompanyContacts;


public static CompanyContactsParser parse(String json) {
    return (CompanyContactsParser) System.JSON.deserialize(json, CompanyContactsParser.class);
}

static testMethod void testParse() {
    String json = '{'+
    '    \"CompanyContacts\": ['+
    '        {'+
    '            \"contact\": \"pn0\",'+
    '            \"postalcode\": \"0\",'+
    '            \"contactnumber\": \"pc0\"'+
    '        },'+
    '        {'+
    '            \"contact\": \"pn1\",'+
    '            \"postalcode\": \"1\",'+
    '            \"contactnumber\": \"pc1\"'+
    '        },'+
    '        {'+
    '            \"contact\": \"pn2\",'+
    '            \"postalcode\": \"2\",'+
    '            \"contactnumber\": \"pc2\"'+
    '        }'+
    '    ]'+
    '}';
    CompanyContactsParser obj = parse(json);
    System.assert(obj != null);
    }
}

The alternative would be using the JSONParser, but this will chew up a LOT of script statements quickly if you have a lot of data, and is also fairly hard to read and maintain


Suman, Your json looks like it has an array of contact,postalcode,contactnumber.

Suppose you get the json from an http response:

HttpResponse resp = h.send(req);

You can start parsing it as below:

JSONParser jsonParser = JSON.createParser(resp.getBody());
 while(jsonParser.nextToken() != null){

 if(jsonParser.getCurrentToken() == JSONToken.START_ARRAY ){
          while(jsonParser.nextToken() != JSONToken.END_ARRAY){
            if((jsonParser.getCurrentToken() == JSONToken.FIELD_NAME) &&(jsonParser.getText() == 'Key')){
               jsonParser.nextToken();
              if(jsonParser.getText()== 'Token1'){
                  jsonParser.nextToken();
               if(jsonParser.getCurrentToken() == JSONToken.FIELD_NAME) &&(jsonParser.getText() == 'Value')){
                 jsonParser.nextToken();
                 uploadToken = jsonParser.getText();
                             }
                 }else if(jsonParser.getText()== 'Token2'){
                      jsonParser.nextToken();
                 if((jsonParser.getCurrentToken() == JSONToken.FIELD_NAME) &&(jsonParser.getText() == 'Value')){
                      jsonParser.nextToken();
                      destFolderPath = jsonParser.getText();
                     }
                  }
                  }
                  }
                  }

Note that here, I'm only interested in getting the values of the two tokens. You can put this inside a loop and get all the values in a list just as easily.

Here's the json I parsed using the above code:

"paramList": [
                              {
                                        "Key": "Token1",
                                        "Value": "xyz"
                              },
                              {
                                        "Key": "Token2",
                                        "Value": "abc"
                              }
                    ]