2014-01-09 199 views
1

我打电话给smartsheet.com api,他们将返回一个对象或数组,我可以用下面的代码将对象处理成一个CF查询,但遇到问题处理数组。ColdFusion JSON对象与对象数组

的JSON:

[ 
{ 
"id": 2070463980562308, 
"name": "Sheet1", 
"accessLevel": "OWNER", 
"permalink": "https://app.smartsheet.com/b/home?" 
}, 
{ 
"id": 2810804673243012, 
"name": "Project Management", 
"accessLevel": "OWNER", 
"permalink": "https://app.smartsheet.com/b/home?" 
}, 
{ 
"id": 3678697304680324, 
"name": " - Dispatch Sheets", 
"accessLevel": "OWNER", 
"permalink": "https://app.smartsheet.com/b/home?" 
} 
] 

我的CF代码:

<cfset jsonData = deserializeJSON(json.smartsheet.sheets.filecontent) /> 

<!--- Check we have records returned to us ---> 
<cfif arrayLen(jsonData.sheets)> 
<!--- We want to provide the query with column names ---> 
<cfset strColType  = '' /> 
<!--- To do this, we'll take the first result item... ---> 
<cfset sheets = jsonData.sheets[1] /> 
<!--- and get the list of keys from the structure. ---> 
<cfset thisKeyList  = structKeyList(sheets) /> 
<!--- 
    We now need to provide the column data type. 
    This example assumes everything is a VarChar. 
    Looping over the list of keys, we'll append a 
    datatype to the column type list defined earlier. 
---> 
<cfloop list="thisKeyList" index="listItem"> 
    <cfset listAppend(strColType,'varChar') /> 
</cfloop> 

<!--- 
    Generate the new query, passing in the 
    column list, column type list and the data. 
---> 
<cfset qrySheets = queryNew(
thisKeyList, 
strColType, 
jsonData.sheets 
) /> 

</cfif> 

此代码只有当我得到一个对象back..not数组工作。

+3

后'deserializeJSON()',JS对象成为CF结构,JS阵列变得CF阵列。没有什么棘手的。 ''并调试你的代码。 – Henry

+0

我明白这不是棘手的问题..我只是没有建立联系。 – AbeS

+0

问问它哪里不适合你,什么是例外,等等。 – Henry

回答

0

它看起来像你发布的JSON代码是为数组而不是对象,所以我会大胆猜测对象返回的内容。

JSON对象是最有可能返回sheets阵列它在其内部要访问作为jsonData.sheets

JSON数组只是片的阵列。所以,试试这个代码:

<!--- default array ---> 
<cfset sheetArray = [] /> 

<cfset jsonData = deserializeJSON(json.smartsheet.sheets.filecontent) /> 

<!--- Check we have records returned to us ---> 

<cfif isArray(jsonData) AND arrayLen(jsonData)> 
    <cfset sheetArray = jsonData /> 
<cfelseif isStruct(jsonData) AND stuctKeyExists(jsonData, "sheets")> 
    <cfif isArray(jsonData.sheets) AND arrayLen(jsonData.sheets)> 
     <cfset sheetArray = jsonData.sheets /> 
    </cfif> 
</cfif> 

<!--- Check we have records returned to us ---> 
<cfif arrayLen(sheetArray)> 
<!--- We want to provide the query with column names ---> 
<cfset strColType  = '' /> 
<!--- To do this, we'll take the first result item... ---> 
<cfset sheets = sheetArray[1] /> 
<!--- and get the list of keys from the structure. ---> 
<cfset thisKeyList  = structKeyList(sheets) /> 
<!--- 
    We now need to provide the column data type. 
    This example assumes everything is a VarChar. 
    Looping over the list of keys, we'll append a 
    datatype to the column type list defined earlier. 
---> 
<cfloop list="thisKeyList" index="listItem"> 
    <cfset listAppend(strColType,'varChar') /> 
</cfloop> 

<!--- 
    Generate the new query, passing in the 
    column list, column type list and the data. 
---> 
<cfset qrySheets = queryNew(
    thisKeyList, 
    strColType, 
    jsonData.sheets 
) /> 

</cfif> 

BTW,queryNew只需要两个参数:QueryNew(columnlist [, columntypelist])所以我不知道如何为你工作(必须忽略额外的参数)。

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f94.html