ColdFusion 11读取多级JSON文件

凯文·邦哈特(Kevin Bomhardt)

我有一个运行中的ColdFusion 11站点,该站点可提取JSON文件并对其进行反序列化,并且能够输出内容。有效的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",
  "start_date":"start date goes here",
  "categories":["cat1","cat2","cat3"]
  }

现在他们给了我一个新的JSON文件,它具有更高的级别,我不知道如何将相同的数据降低到新的级别。

新的JSON

[
 {
  "self_study":
   [
    {
     "categories":["Cat1","Cat2"],
     "link":"some link",
     "initiative":"initiative content goes here",
     "language":"language goes here",
     "name":"name of product",
     "tags":["tag1","tag2","tag3","tag4"],
     "body":"some text goes here"
    }
   ],
  "scheduled":
   [
    {
     "categories":["Cat1","Cat2"],
     "link":"some link",
     "initiative":"initiative content goes here",
     "language":"language goes here",
     "name":"name of product",
     "tags":["tag1","tag2","tag3","tag4"],
     "body":"some text goes here"
    }
   ]
 }
] 

使用第一个JSON文件,我可以使用CFLOOP遍历数据

<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>
#ArrayToList(Course.tags)# <br>
#ArrayToList(Course.categories)# <br>

</cfloop>
</cfoutput>
<!--- End of Code --->

对于CFDUMP,我得到以下结构:

Array(1)
 Struct(scheduled)
   Array(1)
    Struct(my data)
 Struct(self_study)
   Array(1)
    Struct(my data)

关于如何向下浏览多级JSON有任何想法吗?

谢尔泽

在JSON中,数组用方括号[]括起来,而结构用大括号{}括起来。

反序列化JSON的调用为您完成了所有工作。将您的第二个文件带入一个包含结构体数组的结构体数组。

因此,如果要每个数组中的第一个元素:

mydoc[1].self_study[1].categories[1]
mydoc[1].self_study[1].initiative

显然,您可以在数组上使用所有数组操作,而在struct上使用struct操作。希望有足够的信息来帮助您前进。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章