9

how to parse the json string . here multiple contact,multiple postalcode and mulitple contactnumber .how can i get the values from this json output.like list of contacts,list of postalcode and list of contactnumber.

{
    "CompanyContacts": [
        {
            "contact": "pn0",
            "postalcode": "0",
            "contactnumber": "pc0"
        },
        {
            "contact": "pn1",
            "postalcode": "1",
            "contactnumber": "pc1"
        },
        {
            "contact": "pn2",
            "postalcode": "2",
            "contactnumber": "pc2"
        }
    ]
}
12

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.

  • Thanks PJC for your response... i tried what you said i got out put like this from this out put how to get the list<contact>,list<postalcode> and list<contactnumber> pls help how to solve this problem... output: CompanyContacts: [CompanyContacts=(CompanyContactsWrapper:[postalcode=1, contact=pn1, contactnumber=pc1], CompanyContactsWrapper:[postalcode=, contact=, contactnumber=], CompanyContactsWrapper:[postalcode=, contact=, contactnumber=]) ] Thanks – Kittu Mar 7 '13 at 9:51
  • you just loop over your list: for(CompanyContactsWrapper wrapper : companycontacts.CompanyContacts){ wrapper.contact; wrapper.postalcode;} then you can do whatever you want with that data, put it in other lists, make contacts, whatever ;) – pjcarly Mar 7 '13 at 10:26
  • Thank you very much for your valuable input to resolve our constrains..Thanks – Kittu Mar 7 '13 at 10:46
  • @Kittu Good Question – Bforce Feb 11 '14 at 10:54
9

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

3

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"
                              }
                    ]
3

If anyone wants a JSON parsing utility to just extract one or a few values without deserializing to a class then check out

https://github.com/Trigger2991/SFDC-JSON-Parser

It allows you to parse a JSON string and then extract the value of any key you request. Great when calling an API that returns many values and you only want one or two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.