【问题标题】:Create nested JSON structure with Coldfusion使用 Coldfusion 创建嵌套的 JSON 结构
【发布时间】:2013-06-15 20:48:47
【问题描述】:

我已经将 CF 结构等转换为 JSON 有一段时间了,一切都很好。尤其是 Coldbox 使这变得非常容易。

但是,我目前正在使用一个 jQuery 数据表,需要以下面的格式将它传递给 JSON。

我从一组对象开始。

我只希望每个对象中的某些属性进入最终的 JSON 字符串。

我在兜圈子,可能完全将我的数据转换为这种格式的 JSON 过于复杂。任何人都可以提供帮助,或者建议我可以做到这一点的简单方法..

另外值得一提的是,我正在冷箱中构建它。 Coldfusion 9.

{ "aaData": [ [ "Test1", "test@test1", "444444444", "<i class=''icon-pencil icon-large'' data-id=''s1''></i>" ],[ "Test2", "test@test2", "555555555", "<i class=''icon-pencil icon-large'' data-id=''s2''></i>" ],[ "Test3", "test@test3", "666666666", "<i class=''icon-pencil icon-large'' data-id=''s3''></i>" ] ]}

非常感谢!

================================================ =======

这是我需要的游戏代码:

var dataStruct = structNew();
var dataArray = arrayNew(1);
var subsArray = arrayNew(1);
var subs = prc.org.getSubscribers();

for (i=1; i<=arrayLen(subs); i++){
    arrayAppend(subsArray,"#subs[i].getName()#");
    arrayAppend(subsArray,"#subs[i].getEmail()#");
    arrayAppend(subsArray,"#subs[i].getMobile()#");
    arrayAppend(subsArray,"<i class='icon-pencil icon-large' data-id='s#subs[i].getID()#'></i>");
    arrayAppend(dataArray,subsArray);
    arrayClear(subsArray);
};
structInsert(dataStruct,'aaData',dataArray);    
event.renderData('json',dataStruct);

【问题讨论】:

  • 什么版本的 ColdFusion?

标签: json coldfusion coldfusion-9 coldbox


【解决方案1】:

好的,所以你有一个包含对象的数组,并且这些对象包含你需要在这个 JSON 化数组中结束的所有属性,是吗?

这样做:

create a new array
loop over the array of objects
    create a struct
    put all the values from each object you need to go into the JSON; be mindful to use associative array notation when setting the keys, to perserve the case of the keys
    append the struct to the new array
/loop
serializeJson the new array

我认为没有比这更简单的方法了。

【讨论】:

  • 谢谢卡梅伦。这就是我要做的,但是输出 JSON 上的数据不是“名称”:“值”。它只是一个 ov 值的列表。序列化结构会将键和值都放入 JSON。
  • 在这种情况下,只需将“创建结构”步骤替换为“创建数组”,从原始对象中提取数组每个索引的值,以确保值在正确的顺序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2014-03-13
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 2015-01-27
相关资源
最近更新 更多