PDA

View Full Version : How can I reference JSON data in Cold Fusion



HeathT
20 Jul 2007, 11:06 AM
Hey all

I'm an ext newbie here. I've searched the forums and the web but I can't find the answer to my question, so here goes.

How in the world do I reference data in a JSON structure that is sent back to a Cold Fusion template. I'm assuming its a scope but I've tried various scopes to include but not limited to: variables, form, the formfields array, the structure's 'root', I even attempted a "result(s)" scope as I saw php code using that scope and thought CF might have some hidden scope I had never seen. I got nothing.

Basically I want to send the JSON struct to a Cold Fusion template and perform an update and/or an insert from a grid. So, I'm not concerned about how to walk through the data in the structure as I'm certain once I know how to reference them I can handle that.

I'm using YUI + Ext + Cold Fusion. Below is a simplified version of what I'm trying to do.

My structure being sent to Cold Fusion:

[{"description":"A game of tennis","activity":"Tennis"}]

My Cold Fusion code to use the data for an insert:

<cfquery datasource="myDS" name="addActivity">
INSERT into activities
(activity, description)
values
('#<WHAT SCOPE GOES HERE IF ANY>.activity#', '#<WHAT SCOPE GOES HERE IF ANY>.description#');
</cfquery>

mystix
21 Jul 2007, 10:22 AM
this might interest you

http://www.epiphantastic.com/cfjson/examples.php

Chego
21 Jul 2007, 9:20 PM
FYR


<cfsetting enablecfoutputonly="true">
<cfparam name="id" default="1" />
<cfset VARIABLES.userGW = CreateObject("component","getUserGateway").init("YourDSN")>
<cfset VARIABLES.qryRet = VARIABLES.userGW.GetUser(id)>

<cfset jsonStr = StructNew()>
<cfset jsonStr['users'] = arraynew(1)>
<cfset jsonStr.users[1] = StructNew()>
<cfset jsonStr.users[1]['id'] = VARIABLES.qryRet.id>
<cfset jsonStr.users[1]['firstname'] = VARIABLES.qryRet.firstname>
<cfset jsonStr.users[1]['password'] = VARIABLES.qryRet.passwd>
<cfset jsonStr.users[1]['username'] = VARIABLES.qryRet.username>
<cfset jsonStr.users[1]['lastname'] = VARIABLES.qryRet.lastname>

<cfinvoke component="js.cfjson.JSON" method="encode" data="#jsonStr#" returnvariable="result" />
<cfcontent type="text/plain; charset=utf-8"><cfoutput>#result#</cfoutput></cfcontent>
<cfsetting enablecfoutputonly="false">