Groovy - JsonSlurper 解析 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25775267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:31:34  来源:igfitidea点击:

Groovy - JsonSlurper Parsing JSON file

jsongroovy

提问by pinkdevelops

I have a JSON document structured similar to below, and I am trying to parse it in Groovy. Basically for each School (School Info), I want to grab the SCHOOL_COUNTRYand other fields. I am trying this code below but it is not returning what I need. For each school listed (1,000's), I want to grab only specific parts, for instance:

我有一个类似于下面结构的 JSON 文档,我正在尝试在 Groovy 中解析它。基本上对于每个学校(学校信息),我想抓住SCHOOL_COUNTRY和其他领域。我正在尝试下面的代码,但它没有返回我需要的东西。对于列出的每所学校(1,000 所),我只想获取特定部分,例如:

def parseJSON(long id) {

    JSONFile fileInstance = JSONFile.get(id)
    def json = new JsonSlurper().setType(RELAX).parse(new FileReader(fileInstance.filePath))
    def schoolInfo = json.SCHOOL_INFO
    def schoolName = json.SCHOOL_INFO.SCHOOL_NAME
    schoolInfo.each {
       render(schoolInfo.SCHOOL_NAME)
    }
}

So basically for each school, just print out the name of the school. The JSON structure:

所以基本上对于每个学校,只需打印出学校的名称。JSON 结构:

[{
    "SCHOOL_INFO": {
        "SCHOOL_COUNTRY": "Finland",   
        "SCHOOL NAME": "Findland Higher Learning"              
     },
     "LOCATION": {                  
         "LONGITUDE": "24.999",                   
         "LATITUDE": "61.001"
     }
}]

回答by Pawe? Piecyk

I'm not sure if it's the only bug but you can't read schoolInfo.SCHOOL_NAMEin each. SCHOOL_NAMEis property of json.SCHOOL_INFOso it.SCHOOL_NAMEis proper way to access it. Take look at example below:

我不知道这是否是唯一的错误,但你不能阅读schoolInfo.SCHOOL_NAMEeachSCHOOL_NAME是属性,json.SCHOOL_INFO所以it.SCHOOL_NAME是访问它的正确方法。看看下面的例子:

import groovy.json.JsonSlurper

def jsonAsText = '''[{
    "SCHOOL_INFO": {
        "SCHOOL_COUNTRY": "Finland",   
        "SCHOOL NAME": "Findland Higher Learning"              
    },
    "LOCATION": {                  
        "LONGITUDE": "24.999",                   
        "LATITUDE": "61.001"
    }
}]'''

def json = new JsonSlurper().parseText(jsonAsText)

def schoolInfo= json.SCHOOL_INFO
schoolInfo.each{
    println it."SCHOOL NAME"
}

It prints:

它打印:

Findland Higher Learning

回答by Opal

Here You go:

干得好:

import groovy.json.JsonSlurper

def t = """[{
    "SCHOOL_INFO": {
        "SCHOOL_COUNTRY": "Finland",   
        "SCHOOL NAME": "Findland Higher Learning"              
     },
     "LOCATION": {                  
         "LONGITUDE": "24.999",                   
         "LATITUDE": "61.001"
     }
}]"""

def slurper = new JsonSlurper().parseText(t)
slurper.each {
    println it.SCHOOL_INFO."SCHOOL NAME"
}

I'm not sure if there should be _sign in SCHOOL NAME.

我不确定是否应该_SCHOOL NAME 中签名。

回答by lead

println it.SCHOOL_INFO."SCHOOL NAME"

This should work without _ sign.

这应该在没有 _ 符号的情况下工作。