加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用groovy解析json

发布时间:2020-12-14 16:39:42 所属栏目:大数据 来源:网络整理
导读:我想解析的JSON数据,进来就像: { "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","li
我想解析的JSON数据,进来就像:

{
   "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"
   }
}

我使用的代码是:

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

但我不能得到任何分配给Urls。有什么建议么?

解决方法

该响应是一个Map,带有一个关键字为“212315952136472”的元素。地图中没有“数据”键。如果你想遍历所有条目,使用这样:

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

如果你知道它是一个单元素的地图,那么你可以直接访问链接:

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

如果您知道ID(例如,如果您使用它来发出请求),那么您可以更简洁地访问该值:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读