-2

I am trying to Display the JSON Object in VF page but I get the result like this, How do i get the list individually and iterate it in Pageblocktable.

This is the reference link building something like this

But I see the Output like this once i hit Search, This is the Output

And Simple VF

    <apex:page controller="SearchBooks" >
<apex:form >
    <apex:commandButton action="{!callsearch}" value="Search" id="theButton"/>
</apex:form>
<apex:pageBlock title="My Content">
    <apex:pageBlockTable value="{!result}" var="item">
        <apex:column value="{!item}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

This is my Class

public class SearchBooks {
public GoogleBooksResponse result { get; set; }     

 public class Items {
    public String kind {get;set;} 
    public String id {get;set;} 
    public String etag {get;set;} 
    public String selfLink {get;set;} 

}

public searchBooksApi callsearch() {
    String s;
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setHeader('Content-Type','application/json');
    req.setHeader('Accept','application/json');
    req.setEndpoint('https://www.googleapis.com/books/v1/volumes?q=harry+potter');
    Http http = new Http();
    HTTPResponse res = null;
    res = http.send(req);
    s = res.getBody();
    GoogleBooksResponse result = (GoogleBooksResponse)  System.JSON.deserialize(s , GoogleBooksResponse.class);
    this.result = result;
    return null;
}

}

This the GoogleBooksResponse

public class GoogleBooksResponse{
public  String kind;
public  Integer totalItems;
public  List<items> items; //Your existing class.

public class Items {
    public String kind {get;set;} 
    public String id {get;set;} 
    public String etag {get;set;} 
    public String selfLink {get;set;} 

}

}

  • 5
    The answer for your question is there in the link in your question. de-serialize using a wrapper class and display it in UI – RedDevil Jun 25 '18 at 17:19
  • Edit the controller into this post. It may have changed between questions, and you may add or change it in response to comments. – battery.cord Jun 25 '18 at 17:19
  • I have edited my post, But how to iterate each single items? @RedDevil – Arvind Jun 25 '18 at 17:23
  • If I want to display Etag and SelfLink how do i do that? – Arvind Jun 25 '18 at 17:29
  • Please paste the structure of GoogleBooksResponse class. I think it has only one item which is list of items by looking at the output screenshot. – Kiran Machhewar Jun 25 '18 at 17:30
1

In the page block table value you need to assign items variable of each variable. Currently you are assigning the result in your case as per JSON only one value it will contain and the items variable of it contains all the list of Google books. In your case if you use nested page block tables will solve you problem.

    <apex:pageBlockTable value="{!results}" var="res">
            <apex:column value="{!res.kind}"/>
            <!-- Add rest of the columns you want to display of Result attributes-->

            <apex:column breakBefore="true" colspan="2">
                <apex:pageblocksection title="Books Information" showheader="true" collapsible="true" columns="1">
<!-- iterating the books list-->
                    <apex:pageBlockTable value="{!res.items}" var="item">
                        <apex:column value="{!item.kind}"/>
                        <!-- Add rest of the columns you want to display of Items attributes-->
                    </apex:pageBlockTable>
                </apex:pageblockSection>
            </apex:column>
        </apex:pageBlockTable>

If you don't want nested page block table then you can simply assign results[0].items to the value.

The class structure should be like this.

public class GoogleBooksResponse{
public  String kind{get;set;}
public  Integer totalItems{get;set;}
public  List<items> items{get;set;}
}
public class Items {
    public String kind {get;set;} 
    public String id {get;set;} 
    public String etag {get;set;} 
    public String selfLink {get;set;} 

}
  • Tapas, How will be the Controller, Because i get Error: Unknown property 'GoogleBooksResponse.kind' – Arvind Jun 25 '18 at 18:24
  • Kindly declare all the attributes inside GoogleBooksResponse as public, will solve the problem. – Tapas Tripathi Jun 25 '18 at 18:27
  • As you havn't declared any access specifiers to the inside attributes of the Class GoogleBooksResponse so its its setting access specifier as default while creating instance of that class due to which it is not visible to controller/page. – Tapas Tripathi Jun 25 '18 at 18:29
  • Tapas, Please check I have update the question and did the same was as insisted. help me to pass this through. – Arvind Jun 25 '18 at 18:35
  • 1
    Kindly declare the attributes inside GoogleBooksResponse class as getter setter like u did for items class and declare item class outside the GoogleBooksResponse class. – Tapas Tripathi Jun 25 '18 at 18:50
0

Please change the code in below way. GoogleBooksResponse contains list of items, which you need to use in value. In var you get single item and in column value you should be mapping the attribute of single item.

<apex:pageBlock title="My Content">
            <apex:pageBlockTable value="{!result.items}" var="item">
    <!-- as you iterating GoogleBooksResponse it need to be a inner variable of that class -->
                <apex:column value="{!item.<PROPERTY FROM GoogleBooks Class>}"/>
            </apex:pageBlockTable>
</apex:pageBlock>
  • <apex:column value="{!item.<PROPERTY FROM GoogleBooks Class>}"/> What excatly i have to add.. Because i tried {!item.kind} This dint work. Error: Unknown property 'GoogleBooksResponse.items' – Arvind Jun 25 '18 at 18:11
  • you need to past the structure of GoogleBooks. Since I dont the know the structure, I mentioned any property from that class. If you can paste the structure of that class then I can help you with the final code. – Kiran Machhewar Jun 25 '18 at 18:24
0

Please see the updates below. you should not use local variable else your bind variable to page always be null and you need to point the local variable using item.

 public class SearchBooks {
        public GoogleBooksResponse result { get; set; }     

         public class Items {
            public String kind {get;set;} 
            public String id {get;set;} 
            public String etag {get;set;} 
            public String selfLink {get;set;} 

        }

        public searchBooksApi callsearch() {
            String s;
            HttpRequest req = new HttpRequest();
            req.setMethod('GET');
            req.setHeader('Content-Type','application/json');
            req.setHeader('Accept','application/json');
            req.setEndpoint('https://www.googleapis.com/books/v1/volumes?q=harry+potter');
            Http http = new Http();
            HTTPResponse res = null;
            res = http.send(req);
            s = res.getBody();
            // Not a local variable
            result = (GoogleBooksResponse)  System.JSON.deserialize(s , GoogleBooksResponse.class);
            this.result = result;
            return null;
        }

         <apex:page controller="SearchBooks" >
        <apex:form >
            <apex:commandButton action="{!callsearch}" value="Search" id="theButton"/>
        </apex:form>
        <apex:pageBlock title="My Content">
            <apex:pageBlockTable value="{!result}" var="item">
    <!-- as you iterating GoogleBooksResponse it need to be a inner variable of that class -->
                <apex:column value="{!item.kind}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        </apex:page>

I Cannot paste the apex class here Please go here https://json2apex.herokuapp.com/ and create the proper apex class to parse the response

  • I did this @avijit the problem is I get this error Error: Unknown property 'GoogleBooksResponse.kind' – Arvind Jun 25 '18 at 18:04
  • can you post the JSON then only I can help Your class is not correct – Avijit Chakraborty Jun 25 '18 at 18:06
  • googleapis.com/books/v1/volumes?q=harry+potter this is the URL and this is the JSON (once after you click the link) – Arvind Jun 25 '18 at 18:08

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.