0

I get the following struct return from disqus.com API and I just don't know how to retrieve only the following "id" value in red using Coldfusion.

enter image description here

This is the full array returned.

 {
  "cursor":{
    "prev":null,
    "hasNext":false,
    "next":"1213061503000000:1:0",
    "hasPrev":false,
    "total":null,
    "id":"1213061503000000:1:0",
    "more":false
  },
  "code":0,
  "response":[
    {
      "category":"1",
      "reactions":0,
      "identifiers":[],
      "forum":"bobross",
      "title":"Donkeys live a long time",
      "dislikes":0,
      "isDeleted":false,
      "author":"1",
      "userScore":0,
      "id":"2",
      "isClosed":false,
      "posts":0,
      "link":null,
      "likes":0,
      "message":"\"Donkeys live a long time. None of you have ever seen a dead donkey.\"",
      "ipAddress":"127.0.0.1",
      "slug":"donkeys_live_a_long_time",
      "createdAt":"2008-06-10T02:31:43"
    },
    {
      "category":"1",
      "reactions":0,
      "identifiers":[
        "my-identifier"
      ],
      "forum":"bobross",
      "title":"Happy Accidents",
      "dislikes":0,
      "isDeleted":false,
      "author":"1",
      "userScore":0,
      "id":"1",
      "isClosed":false,
      "posts":76,
      "link":null,
      "likes":0,
      "message":"\"If you've painted before you know that we don't make mistakes -- we have happy accidents.\"",
      "ipAddress":"127.0.0.1",
      "slug":"happy_accidents",
      "createdAt":"2008-06-10T01:31:43"
    }
  ]
}
4

1 回答 1

6

嗯:首先,这是一个 JSON 数据包而不是一个数组,所以您需要通过反序列化将其转换为 CFML 数据结构,例如:

data = deserializeJson(jsonPacket);

然后,您将拥有一个本机 CFML 结构(不是数组...数组是结构中的值之一)。

从那里,您可以像通常使用 CFML 结构一样访问任何给定元素,使用结构/数组表示法或结构函数等。

要直接解决您指出的项目,它将是(鉴于上面的代码已首先运行):

data.response[1].id

但是我怀疑你真的不想只解决那个值?但是如果没有更多关于你想要做什么的细节,除了你想要什么之外很难回答。

如果您想获取所有 ID,可以这样做:

ids = [];
for (singleResponse in data.response){
    arrayAppend(ids, singleResponse.id);
}

或者在 ColdFusion 10 上有更多关于如何迭代数组的选项。

再次:澄清你想要做什么,我们可以帮助你做到这一点。

于 2013-08-31T18:11:34.567 回答