简体   繁体   English

如何将JSON脚本作为文件发送到Groovy

[英]How to send a json script as a file to groovy

Below is my JSON script. 以下是我的JSON脚本。 PRAMS.json PRAMS.json

{
    "JSON" : {
        "test": "iTEST",
        "testname": "BOV-VDSL-link-Rateprofile-CLI-Test-1",
        "params": [
            {
                "n2x_variables / config_file": "C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP  Regression/BOV/Bov-data-1-single-rate-profile.xml"
            },
            {
                "n2x_variables / port_list": "303/4 303/1"
            }
        ]
    }
}

Below is my groovy script and I am sending params.json script to the same groovy script. 以下是我的groovy脚本,我正在将params.json脚本发送到相同的groovy脚本。

parseJSON.groovy parseJSON.groovy

import groovy.json.JsonSlurper
def jsonFile = new File("../var/PARAMS.json")
def keys = new JsonSlurper().parse("jsonFile.text")
println keys.keySet()       

I am getting below error : 我得到以下错误:

****No signature of method: groovy.json.JsonSlurper.parse() is applicable for argument types: (java.lang.String) values: [jsonFile.text]****

Can any one please help me ? 谁能帮帮我吗 ?

Thanks for reply, I am new to this json. 感谢您的回复,我是这个json的新手。

I am unable to share screen shot, showing error message when I am trying to upload image but i can give total error message : 我无法共享屏幕截图,尝试上传图像时显示错误消息,但我可以给出总错误消息:

developer@cn-vm-yourname:~/Desktop/kramdeni/vars$ groovy parseJSON.groovy

Caught: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parse() is applicable for argument types: (java.lang.String) values: [jsonFile]
Possible solutions: parse(java.io.Reader), parseText(java.lang.String), use([Ljava.lang.Object;), wait(), grep(), any()
groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parse() is applicable for argument types: (java.lang.String) values: [jsonFile]
Possible solutions: parse(java.io.Reader), parseText(java.lang.String), use([Ljava.lang.Object;), wait(), grep(), any()
at parseJSON.run(parseJSON.groovy:3)

developer@cn-vm-yourname:~/Desktop/kramdeni/v

and my expected output is to print only all values without keys in required order. 我的预期输出是仅按要求的顺序打印所有值而没有键。

To get above result I wrote groovy script like below : 为了获得上述结果,我编写了如下的groovy脚本:

import groovy.json.JsonSlurper

label = "test testname params"

def jsonFile = new File('PARAMS.json')
def par = new JsonSlurper().parse(jsonFile)
println keys.keySet()

def command = ""
keys = label.split(" ")
println "keys: " + keys

for (key in keys) {
    command += par[key] + " "  
}
println "command: " + command
import groovy.json.JsonSlurper

def src = new File("MYPATH/MY.json")
//next line downloads json from URL:
//def src = new URL("http://date.jsontest.com")

def json = new JsonSlurper().parse( src.newInputStream() )

json.each{ k,v->  println "$k = $v" }

try it: 试试吧:

https://groovy-playground.appspot.com/#?load=ccceffd570c6ee176bc6f1fcdafdcbe0 https://groovy-playground.appspot.com/#?load=ccceffd570c6ee176bc6f1fcdafdcbe0


if you have exception 如果有例外

groovy.lang.MissingMethodException: No signature of method:
  groovy.json.JsonSlurper.parse() is applicable for argument types:
    (java.io.BufferedInputStream)
  Possible solutions: parse(java.io.Reader) ...

that means you have quite old version of groovy, however it suggests solution - try to get reader from file: 这意味着您具有相当老版本的groovy,但是它建议了解决方案-尝试从文件中获取阅读器:

def src = new File("MYPATH/MY.json")
def json = new JsonSlurper().parse( src.newReader("UTF-8") )
json.each{ k,v->  println "$k = $v" }

but to continue you have to find version of your groovy, then find documentation for your version and continue developing referencing it 但是要继续,您必须找到groovy的版本,然后找到您版本的文档并继续开发引用它

for example in groovy 1.8.6 there are only two parse methods in JsonSlurper: 例如在groovy 1.8.6中,JsonSlurper中只有两种parse方法:

http://docs.groovy-lang.org/1.8.6/html/api/groovy/json/JsonSlurper.html http://docs.groovy-lang.org/1.8.6/html/api/groovy/json/JsonSlurper.html

and in the latest groovy much more... 在最新的常规中,更多...

http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

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

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