将JSON转换为带有JsonSlurper的Groovy和未知的“字符串” [英] JSON to Groovy with JsonSlurper and unknown "string"

查看:185
本文介绍了将JSON转换为带有JsonSlurper的Groovy和未知的“字符串”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Grails / Groovy应用程序,并且在 params 中包含一个带有string名称( grommet widget 可以改变的成员。也就是说,下次可能是极致缩放。这里是JSON:

  def jx ={
job:42,
params:{
grommet:{name:x,data:y},
widget:{name:a, data:b}
}
}

我正试图弄清楚如何获取字符串索环。代码到目前为止:

  def dalist = new JsonSlurper()。parseText(jx)
println dalist.job //给出:42
println dalist.params //给出:[grommet:[name:x,data:y],widget:[name:a,data:b]]
println dalist.params [0 ] //给出:null

任何想法如何获取字符串 grommet

> JSON对象上的键与JSON对象关联,而不是数组,因此您无法通过索引访问它。 JsonSlurper将JSON对象映射到Groovy Maps,因此您可以通过其键(例如字符串)访问 params dalist.params.grommet ,它会给你地图 [name:'x',data:'y']



要访问 params 上的键,您可以执行 dalist.params。 keySet(),它会给你列表 ['grommet','widget'] 。如果你有兴趣知道 params 键,那应该可以做到。如果由于某种原因需要获取'grommet'字符串,可以通过访问该列表中的第一个元素来完成,即 dalist.params .keySet()[0] ,但我不知道为什么你想知道这一点。我不确定是否可以保证该映射的第一个键始终是'grommet',因为JSON对象是由规范无序的(来自 json.org 一个对象是一组无名称/值对),但是反过来, Groovy maps 有序的(默认实现是LinkedHashMap)...所以我会假定在将JSON解析到Groovy世界时保留顺序,但是我会尽量不要依赖于特定的行为呵呵。


I am writing a Grails/Groovy app and I have a JSON object with a "string" name (grommet and widget) inside the params member that can change. That is, next time it might be acme and zoom. Here is the JSON:

def jx = """{ 
    "job": "42",
    "params": {
        "grommet": {"name": "x", "data": "y"},
        "widget": { "name": "a", "data": "b"}
    } 
}"""

I am trying to figure out how to get the string grommet . Code so far:

def dalist = new JsonSlurper().parseText(jx)
println dalist.job     // Gives: 42
println dalist.params  // Gives: [grommet:[name:x, data:y], widget:[name:a, data:b]]
println dalist.params[0]  // Gives: null

Any idea how to get the string grommet? Iama going to keep hitting my head against a wall.

解决方案

The params key on the JSON object is associated with a JSON object, not an array, so you cannot access it by index. JsonSlurper maps JSON objects to Groovy Maps, so you can access params by its keys, which are strings, e.g. dalist.params.grommet, which will give you the map [name: 'x', data: 'y'].

To access the keys on the params you can do dalist.params.keySet(), which will give you the list ['grommet', 'widget']. If you are interested in just knowing params keys, that should do the trick. If you need to get the 'grommet' string for some reason, you can do it by accessing the first element on that list, i.e. dalist.params.keySet()[0], but i don't know why you would want to know that. And i'm not sure if it is guaranteed that the first key of that map will always be 'grommet', as JSON objects are unordered by the spec (from json.org: An object is an unordered set of name/value pairs), but, in turn, Groovy maps are ordered (the default implementation is LinkedHashMap)... so i would assume that the order is preserved when parsing JSON to the Groovy world, but i'd try not to rely on that particular behavior hehe.

这篇关于将JSON转换为带有JsonSlurper的Groovy和未知的“字符串”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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