[SalesForce] Json deserialize with wrapper class

I have created a wrapper class , below is the class

public class Wrapperclass {

    public String pictureUrl {get;set;}
    public String publicProfileUrl {get;set;}

    public class basicdetails{
        public string distance {get;set;}
        public string emailAddress {get;set;}
        public string firstName {get;set;}
        public string headline {get;set;}
        public string lastName {get;set;}
        public location locdetails {get;set;}
    }

    public class positions{
        public integer total {get;set;}
        public List<values> comvalues {get;set;}
    }

    public class location{
        public country countrydetails {get;set;}
        public string name {get;set;}
    }

    public class country{
        public String code {get;set;}
    }

    public class values {
        public company companydetails {get;set;}
        public integer Id {get;set;}
        public boolean iscurrent {get;set;}
        public string title {get;set;}
    }

    public class company{
        public integer Id {get;set;}
        public String industry {get;set;}
        public String name {get;set;}
        public String size {get;set;}
        public String ticker {get;set;}
        public String type {get;set;}
    }
}

And the Json that has been parsed with this class is :

{
    "distance": 0,
    "emailAddress": "surface_alisar@hotmail.com",
    "firstName": "James",
    "headline": "The World news",
    "lastName": "Doe",
    "pictureUrl": "https://media.com/mpr/mprx/0__HjzfUue5D1Wo4-9uMLNI",
    "positions": {
        "_total": 1,
        "values": [
            {
                "company": {
                    "id": 876541,
                    "industry": "Information Technology & Services",
                    "name": "Telsa Inc",
                    "size": "400+",
                    "ticker": "TLA",
                    "type": "Public Domain"
                },
                "id": 8766544532,
                "isCurrent": true,
                "location": {},
                "title": "Engineer Department"
            }
        ]
    },
    "publicProfileUrl": "https://www.zip.com/9875/usad"
}

I need to display these values in the page so in the controller class i wrote

Controller:
    public Wrapperclass wrapperinstance {get;set;}
    wrapperinstance = (Wrapperclass )JSON.deserialize(Jsonstring, Wrapperclass.class); // Jsonstring is the above JSON
System.debug(wrapperinstance);

In the above debug i got only

  ]|DEBUG|wrappercon: -Wrapperclass: [
        pictureUrl=https://media.com/mpr/mprx/0__HjzfUue5D1Wo4-9uMLNI,
        publicProfileUrl=https://www.zip.com/9875/usad
    ]

Why the other values are not parsing/i am not seeing those values.

I am not getting any error; also if i want to use wrapperinstance.basicdetails in the class, it says

Error: VFcontroller Compile Error: Variable does not exist: basicdetails

Best Answer

Your wrapper class and related classes need to look like this:

public class Location {

}
public class Company {
    public Integer id;
    public String industry;
    public String name;
    public String size;
    public String ticker;
    public String type;
}
public class Values {
    public Company company;
    public Integer id;
    public Boolean isCurrent;
    public Location location;
    public String title;
}
public class Positions {
    // public Integer _total; // Sorry, can't have _ as the first character
    public Values[] values;
}
public class WrapperClass {
    public Decimal distance;

    public String emailAddress;
    public String firstName;
    public String headline;
    public String lastName;
    public String pictureUrl;
    public String publicProfileUrl;

    public Positions positions;
}

This was derived from the original JSON. Consider using JSON2Apex in the future to automatically generate the correct code from your JSON.

Related Topic