I want to parse JSON data which is coming in like:

{
   "212315952136472": {
      "id": "212315952136472",
      "name": "Ready",
      "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/195762_212315952136472_4343686_s.jpg",
      "link": "http://www.hityashit.com/movie/ready",
      "likes": 5,
      "category": "Movie",
      "description": "Check out the reviews of Ready on  http://www.hityashit.com/movie/ready"
   }
}

The code I am using is:

JSONElement userJson = JSON.parse(jsonResponse)
userJson.data.each {
    Urls = it.link
}

但是我不能得到分配给Urls的任何东西.有什么建议吗?

推荐答案

该响应是一个映射,带有一个带有键"212315952136472"的元素. map 上没有"数据"键.如果要循环浏览所有条目,请使用以下方法:

JSONObject userJson = JSON.parse(jsonResponse)
userJson.each { id, data -> println data.link }

If you know it's a single-element Map then you can directly access the link:

def data = userJson.values().iterator().next()
String link = data.link

And if you knew the id (e.g. if you used it to make the request) then you can access the value more concisely:

String id = '212315952136472'
...
String link = userJson[id].link

Json相关问答推荐

使用Shell深入挖掘到最低的SON元素

当有嵌套数组而没有嵌套数组时,展平JSON

删除JSON文件的特定内容

使用JQ从jsonl文件中删除具有匹配键/值的行

PowerShell:使用JSON原生的Short命令处理JSON?

Jolt-Json转换:通过引用标识符(而不是索引)设置值

基于 JSON 字段的 Jolt 条件标志

使用 JOLT 将日期格式转换为 JSON

使用 jq,如何将两个属性构成键的对象数组转换为对象的索引对象?

匹配来自不同数组的值

如何在JQ中展平多维数组

无法在 json --data 中使用变量

如何从条带订阅响应对象中正确获取 struct 项?

Rails 控制器无需编码即可渲染 json

Play Framework:如何序列化/反序列化与 JSON 的枚举

如何从Typescript 中的json响应中获取日期对象

waitUntilAllTask​​sAreFinished 错误 Swift

NSManagedObject 属性值的 NSNull 处理

如何使用 Jackson 的 objectMapper 反序列化接口字段?

我如何反序列化以杰克逊为单位的时间戳?