简体   繁体   中英

How to send a json script as a file to groovy

Below is my JSON script. 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.

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.

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 :

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


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:

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

for example in groovy 1.8.6 there are only two parse methods in JsonSlurper:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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