在 Coldfusion 中使用 JSON 数据 [英] Using JSON Data with Coldfusion

查看:20
本文介绍了在 Coldfusion 中使用 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去使用过 JSON 数据 - 主要是捏造"我的解决方案,并没有真正理解事情为什么或如何工作.我遇到了一个问题,即返回的数据看起来与我之前看到的有些不同,而且我找不到任何与之匹配的示例.

I've worked with JSON data in the past - mainly 'fudging' my way to a solution, and not really understanding why or how things work. I've come across an issue where the data being returned looks somewhat different to what I've seen before, and I can't find any examples that match it.

这是返回数据的示例(通过 API);

Here's an example of the data returned (via an API);

{"domain.co.uk":{"status":"available","classkey":"thirdleveldotuk"},"domain.net":{"status":"available","classkey":"dotnet"},"domain.com":{"status":"available","classkey":"domcno"}}

在我的前端,我需要返回这样的东西 -

On my front-end, I need to return something like this -

  • domain.co.uk - 可用
  • domain.net - 可用
  • domain.com - 可用

因为 'domain.com' 等值总是会改变,所以我无法像往常一样映射名称(尽管它总是会返回 3 'rows')

Because the 'domain.com' etc value will always change, I can't map the names as I usually would (although it will always be 3 'rows' returned)

我检查了我拥有的每一本 CF 书籍,并阅读了在线 CF 文档,但我完全不知道从哪里开始阅读这本书!

I've checked every CF Book I own, and read the online CF Docs, but I'm totally at a loss as to where to even start with this one!

非常感谢您的指点!

推荐答案

如果你使用 deserializeJSON(data) 运行它,你会看到你最终得到的是带有嵌套结构的结构.因此,您可以遍历您的结构,获取键,然后获取该键的状态.在 JSON 术语中,您的 JSON 对象具有嵌套对象.

If you run this with deserializeJSON(data), you'll see that you just end up with structures with nested structures. So, you can loop through your struct, grab the keys and then grab that key's status. In JSON terms, your JSON object has nested objects.

<cfset data = deserializeJSON(apiData) />
<cfset formattedData = [] />
<cfset tmp = {} />

<cfloop collection=#data# item="domain">
    <cfset tmp.domain = domain />
    <cfset tmp.status = data[domain]["status"] />
    <cfset arrayAppend(formattedData,duplicate(tmp)) />
</cfloop>

<cfdump var=#formattedData# />

这篇关于在 Coldfusion 中使用 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆