如何阅读JSON数据

use*_*264 0 arrays coldfusion jquery json

这可能是一个愚蠢的问题,但我真的希望有人可以帮助我.

我目前正在serializeJson()ColdFusion 10中使用生成JSON数据集,以便我可以在jquery表插件中处理数据.serializeJson()生成以下JSON数据:

{
-COLUMNS: [
"AMOUNT"
"AMOUNTPAID"
"ENTITY"
"ENTITY_NAME"
"FORMULACURRENCY"
"INTERNALID"
"TRANDATE"
"TRANID"
"TYPE"
"TYPE_INTERNALID"
]
-DATA: [
...
Run Code Online (Sandbox Code Playgroud)

我的插件似乎只处理非常基本的JSON.

[{"rank":1,"title":"The Avengers (2012)","weekend":"$103M","gross":"$373M","weeks":2},
            {"rank":2,"title":"Dark Shadows (2012)","weekend":"$29.7M","gross":"$29.7M","weeks":1},
            {"rank":3,"title":"Think Like a Man (2012)","weekend":"$5.82M","gross":"$81.4M","weeks":4},
            {"rank":4,"title":"The Hunger Games (2012)","weekend":"$4.51M","gross":"$387M","weeks":8},
            {"rank":5,"title":"The Lucky One (2012)","weekend":"$4.11M","gross":"$53.8M","weeks":4},
            {"rank":6,"title":"The Five-Year Engagement (2012)","weekend":"$3.31M","gross":"$24.6M","weeks":3},
            {"rank":7,"title":"The Pirates! Band of Misfits (2012)","weekend":"$3.14M","gross":"$23M","weeks":3},
            {"rank":8,"title":"The Best Exotic Marigold Hotel (2011)","weekend":"$2.67M","gross":"$3.74M","weeks":2},
            {"rank":9,"title":"Chimpanzee (2012)","weekend":"$1.76M","gross":"$25.7M","weeks":4},
            {"rank":10,"title":"Safe (2012)","weekend":"$1.45M","gross":"$15.7M","weeks":3}];
Run Code Online (Sandbox Code Playgroud)

如何仅从ColdFusion的serializeJson()函数获取数据的数组,而不是列表或任何其他元数据?或者,是否有一种简单的方法只从数据数组中获取内容,而没有其他数组?

Ada*_*ron 6

你的问题有点模糊,但我会尽力回答.

serializeJson()并且deserializeJson()只做他们在锡上所说的内容:将原生CFML数据结构转换为JSON格式的字符串.它们本身并不决定翻译的结构.

您的jQuery插件需要一个JavaScript对象数组,这意味着您需要创建JavaScript对象和数组.关于如何在CFML类型和JSONified JS类型之间转换数据的一点是,CFML结构将序列化为JS对象.

因此,如果您从CFML记录集开始......您需要考虑如何使用它来创建结构数组.我建议你为每一行都有一个结构,每个行都是数组中的一个元素.

所以你会想要这样的东西:

create an array
loop over the recordset
    create a struct out of each column value of the current row
    append that struct to the array
/loop
serialise the array to JSON
Run Code Online (Sandbox Code Playgroud)

没有一次性/内置功能可将记录集对象转换为结构数组.你需要手工完成.