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

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

问题描述

过去我曾经使用过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.com - 可用

因为domain.com更改,我不能映射名称,因为我通常会(尽管它将永远是3'行返回)

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 Book我拥有,并阅读在线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 (数据),你会看到你最终得到具有嵌套结构的结构。所以,你可以循环通过你的结构,抓住键,然后抓住该键的状态。在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# />

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

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