将 Coldfusion 查询结果转换为格式化的 JavaScript 数组
我正在尝试编写 ColdFusion 代码运行一个查询,然后将结果以某种格式放入数组中。
查询:
<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
FROM myTable
</cfquery>
和包含 1 个元素的数组如下所示:
<script type="text/javascript">
var addresses = [{ name: "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>", to: #cboxadd#", Barrigada Guam, 96910" }];
</script>
如何实现此目的?
I am trying to write ColdFusion code runs a query then puts the results in an array in a certain format.
the query:
<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
FROM myTable
</cfquery>
and array with 1 element looks like:
<script type="text/javascript">
var addresses = [{ name: "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>", to: #cboxadd#", Barrigada Guam, 96910" }];
</script>
How do I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从我看来,你的对象看起来不对,但我不确定你在用它做什么,所以我不确定如何使它适合你,所以我只有一个较小的例子。
这将构建一个对象数组,每个对象都有属性“name”和“type”,其值是从查询中填充的。
From what I see your object looks wrong, but I'm not sure what you are doing with it, so I'm unsure of how to make it right for you, so I just have a smaller example.
This will build an array of objects, each object has the properties 'name' and 'type' with values populated from the query.
您可以尝试 CF8 或 CF9 中的 serializeJSON 函数,它将获取任何 ColdFusion 对象并将其转换为可用的 JSON 字符串。然后可以在您的 JavaScript 函数中使用它。
You could try the serializeJSON function in CF8 or CF9, which will take any ColdFusion object and turn it into a usable JSON string. That can then be used in your JavaScript functions.
扩展 Dan 所说的内容,我会做这样的事情(假设您没有使用 CFC):
由于我很长一段时间没有使用标签,代码中可能有一些语法错误,但您应该了解要点我想在这里完成的事情。首先,我们将创建一个函数来将查询的输出解析为结构,然后将该结构传递给 sealalizeJSON,这将为您提供一个格式良好的 JSON 对象(请注意,调用后键的大小写可能会有所不同) searilizeJSON)。那么您在页面上所要做的就是:
Expanding on what Dan said I'd do something like this (assuming that you are not using CFCs):
Since I haven't used tags in a long time the code may have a few syntax errors in it, but you should get the gist of what I'm trying to accomplish here. First we'll create a function to parse the output of your query into a structure, then we'll pass that structure to searalizeJSON, which will give you a nicely formatted JSON object (note that the case of the keys may be different after calling searilizeJSON). Then all you'd have to do on your page is: