Monday, March 26, 2012

Json in Apex class and Visualforce

Serializing JSON from Apex 
Apex Class:

public with sharing class jsonAccount {
public string AccountNameList {get;set;}
 public jsonAccount() {
    list<Account> accList = [Select Id,Name From Account]; 
    System.debug(JSON.serialize(accList));
    AccountNameList = JSON.serialize(accList);
 }

}
Output:
USER_DEBUG [2]|DEBUG|[{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBXIA0"},"Id":"001U0000002LdBXIA0","Name":"GenePoint"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBYIA0"},"Id":"001U0000002LdBYIA0","Name":"United Oil & Gas, UK"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBZIA0"},"Id":"001U0000002LdBZIA0","Name":"United Oil & Gas, Singapore"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBaIAK"},"Id":"001U0000002LdBaIAK","Name":"Edge Communications"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBbIAK"},"Id":"001U0000002LdBbIAK","Name":"Burlington Textiles Corp of America"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBcIAK"},"Id":"001U0000002LdBcIAK","Name":"Pyramid Construction Inc."},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBdIAK"},"Id":"001U0000002LdBdIAK","Name":"Dickenson plc"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBeIAK"},"Id":"001U0000002LdBeIAK","Name":"Grand Hotels & Resorts Ltd"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBfIAK"},"Id":"001U0000002LdBfIAK","Name":"Express Logistics and Transport"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBgIAK"},"Id":"001U0000002LdBgIAK","Name":"University of Arizona"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBhIAK"},"Id":"001U0000002LdBhIAK","Name":"United Oil & Gas Corp."},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002LdBiIAK"},"Id":"001U0000002LdBiIAK","Name":"sForce"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000002bHvsIAE"},"Id":"001U0000002bHvsIAE","Name":"AAA Copr"},{"attributes":{"type":"Account","url":"/services/data/v24.0/sobjects/Account/001U0000005sN4LIAU"},"Id":"001U0000005sN4LIAU","Name":"Test BBB"}]
VisualForce Page:

<apex:page controller="jsonAccount">
<apex:form id="qsgFrm">
<apex:outputLink onclick="getAccountName();"> Click and Get the Account Using JSON</apex:outputLink>
</apex:form>
<script>
<script>
//JSON Code
var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
 var t = typeof (obj);
 if (t != "object" || obj === null) {
  // simple data type
  if (t == "string") obj = '"'+obj+'"';
  return String(obj);
 }
 else {
  // recurse array or object
  var n, v, json = [], arr = (obj && obj.constructor == Array);
  for (n in obj) {
   v = obj[n]; t = typeof(v);
   if (t == "string") v = '"'+v+'"';
   else if (t == "object" && v !== null) v = JSON.stringify(v);
   json.push((arr ? "" : '"' + n + '":') + String(v));
  }
  return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
 }
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
 if (str === "") str = '""';
 eval("var p=" + str + ";");
 return p;
};
// GETING ACCOUNT DETAILS
var jsonobj = {!AccountNameList};
//To convert an object to JSON, use JSON.stringify:
var json_text = JSON.stringify(jsonobj, null, 2);
//To convert a JSON string to a JS object, use JSON.parse:
var your_object = JSON.parse(json_text);

 function getAccountName() { 
 for(var f=0; f < your_object.length; f++) {
  var aID = your_object[f].Id;
  var aName = your_object[f].Name;
 }
}
</script>
</apex:page>

1 comment:

  1. What was the point. Couldnt you have easily done this same thing in Apex?

    ReplyDelete