4

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

3

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.

0

You are getting this error because basicDetails is a subclass, and you haven't added it as a property anywhere. Also, your JSON structure and your Apex structure don't match up. Remove your subclass declaration and it should resolve the issue.

Change:

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;}
}

to:

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;}
  • That still won't quite fix the problem, but it'll get closer. – sfdcfox May 31 '17 at 20:03
  • JSON2Apex is realy a good tool. I got it corrected with the right variable names. The names are different and hence facing issues. Thanks guys – Abhik Dey Jun 1 '17 at 7:05

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.