0

I have no clue why I'm getting the error and I have tried all sorts of approach but still no success, here is my source code, I'm trying to parse the json data into the list and use that in pageblockTable.

Error:

Unknown property 'JSON2Apex.Contact.id'

public class HttpCalloutsController {

    public List<JSON2Apex.Contact> jsonRecords {get;set;}    
    public HttpCalloutsController() {

        //HttpRequest class:
        HttpRequest hr = new HttpRequest();
        hr.setEndpoint(Label.EndPointContactURL);
        hr.setMethod('GET');

        //HttpResponse class:
        Http h = new Http();
        HttpResponse res = h.send(hr);
        String jsonString = res.getBody(); 

        JSON2Apex myClass = JSON2Apex.parse(jsonString); 
        jsonRecords = new List<JSON2Apex.Contact>(myClass.Contacts);
    } 
}

Visualforce page:

<apex:page controller="HttpCalloutsController" tabStyle="Contact" >
    <apex:form>
        <apex:pageBlock title="REST Contacts"> 
            <apex:pageBlockTable value="{!jsonRecords}" var="c">
                <apex:column headerValue="Contact Id" value="{!c.id}"/>
                <apex:column headerValue="Contact Name" value="{!c.name}"/>
                <apex:column headerValue="Contact Email" value="{!c.email}"/>
                <apex:column headerValue="Contact Address" value="{!c.address}"/>
                <apex:column headerValue="Contact Gender" value="{!c.gender}"/>
                <!--apex:column headerValue="Contact Phone" value="{!c.phone}"/-->
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

My Json class:

public class JSON2Apex {

    public class Contact {
        public String id;
        public String name;
        public String email;
        public String address;
        public String gender;
        //public Phone phone;
    }

    public List<Contact> contacts; 
    /*public class Phone {
        public String mobile;
        public String home;
        public String office;
    } */
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

I'm following this blog where it talks about easy parsing JSON string

2

If you want to access a property in your Visualforce markup, it always needs a getter.

public class Contact
{
    public String id { get; private set; }
    public String name { get; private set; }
    // etc.
}

The syntax as generated by JSON2Apex doesn't support this behavior out of the box, but it's an easy adjustment to make.

An alternative to the above syntax would be to add actual methods to get the data. From the perspective of the markup, it's functionally identical:

public class Contact
{
    final String id, name, email, etc;
    public String getId() { return id; }
    public String getName() { return name; }
    // etc.
}

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.