显示JSON数组数据时出错-复杂对象类型无法转换为简单值
我目前有一个MoocJson.json文件,如下所示:显示JSON数组数据时出错-复杂对象类型无法转换为简单值,json,multidimensional-array,coldfusion,coldfusion-11,Json,Multidimensional Array,Coldfusion,Coldfusion 11,我目前有一个MoocJson.json文件,如下所示: [ {"body":"some text goes here", "link":"a link is here", "name":"name of product goes here", "language":"language goes here", "tags":["tag1","tag2","tag3","tag4"], "initiative":"initiative content goes here",
[
{"body":"some text goes here",
"link":"a link is here",
"name":"name of product goes here",
"language":"language goes here",
"tags":["tag1","tag2","tag3","tag4"],
"initiative":"initiative content goes here",
"start_date":"start date goes here",
"categories":["cat1","cat2","cat3"]
},
{"body":"2 some text goes here",
"link":"2 a link is here",
"name":"2 name of product goes here",
"language":"2 language goes here",
"tags":["2 tag1","2 tag2","2 tag3","2 tag4"],
"initiative":"2 initiative content goes here",
"start_date":"2 start date goes here",
"categories":["2 cat1","2 cat2","2 cat3"]
},
{"body":"3 some text goes here",
"link":"3 a link is here",
"name":"3 name of product goes here",
"language":"3 language goes here",
"tags":["3 tag1","3 tag2"],
"initiative":"2 initiative content goes here",
"start_date":"2 start date goes here",
"categories":["3 cat1"]
}
]
// End of JSON file
我的CF代码如下:
<cffile action="read" file="#ExpandPath("./MoocJson.json")#" variable="myxml">
<cfset mydoc = deserializedJSON(myxml)>
<cfdump var="#mydoc#"> <!--- this dumps out the JSON in Array format --->
<cfoutput> My Doc Length = #arraylen(mydoc)#</cfoutput>
<!--- Loop through Array of mydoc and out put content --->
<cfoutput>
<cfloop from="1" to="#arrayLen(mydoc)#" index="i">
<cfset Course = mydoc[i]>
#Course.Name# <br>
#Course.body# <br>
#Course.language# <br>
#Course.link# <br>
#Course.initiative# <br>
#Course.start_date# <br>
#Course.tags# <br>
#Course.categories# <br>
</cfloop>
</cfoutput>
<!--- End of Code --->
我的文档长度=#arraylen(我的文档)#
#课程名称#
#当然。身体#
#课程语言#
#课程链接#
#当然,主动性#
#课程开始日期
#课程标签#
#课程类别#
当我运行它时,除了标签和类别之外的所有内容都会显示出来。对于这两个,我得到了一个错误
无法将复杂对象类型转换为简单值
如何读取主数组中的标记和类别数组?标记和类别是数组。可以使用该函数将它们转换为字符串 试试这个:
#ArrayToList(Course.tags)# <br>
#ArrayToList(Course.categories)# <br>
#数组列表(Course.tags)#
#阵列列表(课程类别)#
cf输出只能处理简单值(字符串、数字等)。正如错误消息所说,一些变量,即标记
和类别
包含复杂的值,如数组、结构等,不能用cfoutput显示。您需要将这些数组转换为简单的值,或者在复杂对象ie数组中循环并输出每个元素(假设数组元素是简单的字符串)。另外,在CF11中,您可以使用数组循环,即
,而不是从/到循环。此外,如果这只是为了调试目的,以确保获得所有信息,那么用
替换所有信息就更容易了。