ColdFusion JSON对象与数组对象 [英] ColdFusion JSON object vs array of objects
问题描述
我正在调用smartsheet.com api,它们将返回一个对象或数组,我可以使用以下代码将该对象处理为CF查询,但处理数组时遇到问题。
I'm calling the smartsheet.com api and they will either return an object or an array, I'm able to process the object into a CF Query with the following code, but having trouble processing the array.
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>
此代码只有在我获得一个对象后才能工作。 b $ b
This code only works when I get an object back..not an array.
推荐答案
看来,你发布的JSON代码是为数组而不是对象,所以我会大胆猜测对象返回什么。
It looks like the JSON code you posted is for the array and not the object so I'll take a wild guess at what the object is returning.
JSON对象很可能返回一个 sheet
数组, $ c> jsonData.sheets
The JSON object is most likely returning a sheets
array inside of it which you are accessing as jsonData.sheets
JSON数组只是工作表数组。所以试试这个代码:
The JSON array is just the array of sheets. So try this code:
<!--- 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] )
,所以我不知道这是如何工作的(必须忽略附加参数)。
BTW, queryNew only takes two parameters: QueryNew(columnlist [, columntypelist])
so I'm not sure how that is working for you (must be ignoring the additional parameter).
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f94 .html
这篇关于ColdFusion JSON对象与数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!