简体   繁体   English

JSON到Groovy与JsonSlurper和未知的“字符串”

[英]JSON to Groovy with JsonSlurper and unknown “string”

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. 我正在编写一个Grails / Groovy应用程序,我有一个JSON对象,其中包含可以更改的params成员中的“字符串”名称( 索引小部件 )。 That is, next time it might be acme and zoom . 也就是说,下次它可能是极致缩放 Here is the JSON: 这是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. Iama不停地撞在墙上。

The params key on the JSON object is associated with a JSON object, not an array, so you cannot access it by index. JSON对象上的params键与JSON对象关联,而不是与数组关联,因此您无法通过索引访问它。 JsonSlurper maps JSON objects to Groovy Maps, so you can access params by its keys, which are strings, eg dalist.params.grommet , which will give you the map [name: 'x', data: 'y'] . JsonSlurper将JSON对象映射到Groovy Maps,因此您可以通过其键来访问params ,这些键是字符串,例如dalist.params.grommet ,它将为您提供地图[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'] . 要访问params上的键,您可以执行dalist.params.keySet() ,它将为您提供列表['grommet', 'widget'] dalist.params.keySet() ['grommet', 'widget'] If you are interested in just knowing params keys, that should do the trick. 如果你对只知道params键感兴趣,那应该可以解决问题。 If you need to get the 'grommet' string for some reason, you can do it by accessing the first element on that list, ie dalist.params.keySet()[0] , but i don't know why you would want to know that. 如果由于某种原因需要获取'grommet'字符串,可以通过访问该列表中的第一个元素来完成,即dalist.params.keySet()[0] ,但我不知道为什么你想要我知道。 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. 而且我不确定是否可以保证该映射的第一个键始终是'grommet' ,因为JSON对象是规范的无序(来自json.org对象是一组无序的名称/值对 )但是,反过来,Groovy映射有序的(默认实现是LinkedHashMap)...所以我假设在解析JSON到Groovy世界时保留了顺序,但我尽量不依赖于那个特定的行为嘿嘿。

It's Map instance, try: 这是Map实例,请尝试:

def params = dalist.params.entrySet() as List  // entrySet() returns Set, but it's easier to use it as a List
println params
println params.size()
println params[0]
println params[0].key
println params[0].value

This might help you. 这可能对你有帮助。

import groovy.json.JsonSlurper;
def jx='{"job":"42","params":{"grommet":{"name":"x","data":"y"},"widget":{"name":"a","data":"b"}}}'

def dalist = new JsonSlurper().parseText( jx )

assert dalist.params.getClass().name == "java.util.HashMap";
assert dalist.params.size() == 2;

def keys = dalist.params.collect{ a, b -> a}; // returns "[grommet, widget]"
assert !!dalist.params.get( "grommet" ) == true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM