开发者

Salesforce - Create JSON from SOQL results

开发者 https://www.devze.com 2023-03-23 02:46 出处:网络
I\'m using a VisualForce page to generate a JSON file that\'s read by a jQuery plugin. My page uses a controller to query for records and output them. Code is below:

I'm using a VisualForce page to generate a JSON file that's read by a jQuery plugin. My page uses a controller to query for records and output them. Code is below:

EDIT

Sol开发者_开发问答ved! Here is the solution I found (credit http://blog.lopau.com/visualforce-row-count/)

I made my opp object into a list by changing

 public Opportunity opp {get; private set;}

to

 public Opportunity[] opp {get; private set;}

then used an apex:repeat to loop through the values.

<apex:page standardstylesheets="false" controller="myOppCon" sidebar="false" showHeader="false" contentType="application/x-JavaScript; charset=utf-8">
[
<apex:variable value="1" var="rowNum"/>
<apex:variable var="rawData" value="opp" />
<apex:repeat value="{!opp}" var="List" id="theRepeat">
{
"id":"{!List.id}",
"title":"{!List.name}",
"start":"<apex:outputText value="{0,date,E',' dd MMM yyyy HH:mm:ss z}">
            <apex:param value="{!List.Trip_Start_DateTime__c}" /> 
         </apex:outputText>",
"end":"<apex:outputText value="{0,date,E',' dd MMM yyyy HH:mm:ss z}">
            <apex:param value="{!List.Trip_End_DateTime__c}" /> 
         </apex:outputText>",
"url":"/{!List.id}"
}, 
<apex:variable var="rowNum" value="{!VALUE(rowNum) + 1}"/>
</apex:repeat>
{}]
</apex:page>


Since the Winter12 release, salesforce apex's has native serialie/deserialize JSON methods documented at : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_json.htm

main functions are

JSON.serialize 
JSON.deserialize


In your controller you could make use of the following serialization class https://github.com/SimonGoodyear/sobject-serialization

Then you could simply expose the resulting string as a property that you can access from your VF page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号