Back to Top

Generate JSON data and Convert in PageBlockTable

Updated 16 July 2021

In this post, I am going to share the information how to Generate JSON data and Convert in PageBlockTable and display the same in Visual Force page in tabular format.

JSON ( Java Script Object Notation )

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

The Apex JSON classes provide easy-to-use means of serializing/deserializing Apex objects into/from JSON content. This article explained the basics about handling JSON content in Apex. The code samples provided in the article demonstrated how different serialization and deserialization options could be implemented in Apex.

1. JSON has a smaller footprint than XML, which means it can be transmitted and parsed faster than XML.

2. JSON.serialize() is used to generate JSON.

Searching for an experienced
Salesforce Company ?
Find out More

3. It Simply have a key and value pair combination.

4. DataTypes supported by the JSON are “Number, String, Boolean, Array, Object and null”.

5. Key and value pair are separated by colon “:”

6. Object is enclosed between curly brackets “{” and “}”.

7. Array is enclosed using square bracket “[“, “]” separated by the comma.

Serialization and Deserialization

Apex Code :

public with sharing class jsongenerator {
    
    /**
    * Webkul Software.
    *
    * @category  Webkul
    * @author    Webkul
    * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
    * @license   https://store.webkul.com/license.html
    */

    public   string jsondata{get;set;}
    public list<jsonvalue> jsonstring { get; set; }

    public jsongenerator(){
     string soql ='select name,industry from account';
        List<Account> acct = Database.Query(soql);
        jsondata = JSON.serialize(acct);
        jsonstring = (List<jsonvalue>) System.JSON.deserialize(jsondata , List<jsonvalue>.class);
                
    }
    public class jsonvalue{

                public String name{get;set;}
                public String industry{get;set;}
             
    }
}

Visualforce Page:

<apex:page controller="jsongenerator" showHeader="false">

    <!-- 
    /**
    * Webkul Software.
    *
    * @category  Webkul
    * @author    Webkul
    * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
    * @license   https://store.webkul.com/license.html
    */
    -->
 
    {!jsondata}
    <apex:pageblock >
        <apex:pageblockTable value="{!jsonstring}" var="json">
            <apex:column headerValue="Name" value="{!json.name}"/>
            <apex:column headerValue="Industry" value="{!json.industry}"/>
        </apex:pageblockTable>
    </apex:pageblock>
    
</apex:page>

Json Data in Pageblock Table

 jsondata

Support

That’s all forGenerate JSON data and Convert in PageBlockTable  in Salesforce, still have any issue feel free to add a ticket and let us know your views to make the product better http://webkul.com/ticket/index.php

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content