我需要在Groovy中检查字符串是否是有效的JSON.我的第一个想法是发送它new JsonSlurper().parseText(myString)
,如果没有例外,则认为它是正确的.
但是,我发现Groovy很乐意接受尾随逗号JsonSlurper
,但JSON 不允许使用尾随逗号.有没有一种简单的方法来验证Groovy中的JSON是否符合官方JSON规范?
在代码领域遇到大麻烦,因此我需要帮助!
使用Groovy和JsonSlurper我正在处理下面表单的JSON.当"类型"键设置为某个值时,我正在寻找外部包含元素(在我的情况下,这应该是全部应该是地图).例如,如果类型是"Type5",那么我需要返回三个地图:包含外部Type5的"body"地图,包含INNER Type5的"body"地图和靠近底部的Type5地图(每个的EntrySet也可以正常工作).Type3和Type4表现出相同的行为!
根据请求编辑以拥有有效的Json
我通过Groovy的JsonSlurper运行它,所以它应该是有效的.
{
"type": "Run1",
"body": [
{
"type": "Type1",
"expression": {
"type": "Type2",
"expressions": [
{
"type": "Type3",
"body": {
"type": "Type4",
"id": null,
"params": [
{
"type": "Identifier",
"name": "a"
},
{
"type": "Identifier",
"name": "b"
},
{
"type": "Identifier",
"name": "c"
}
],
"body": {
"type": "Type5",
"body": [
{
"type": "Type6",
"id": {
"type": "Identifier",
"name": "d"
},
"params": [
{
"type": "Identifier",
"name": "a"
}
],
"body": {
"type": "Type5",
"body": [ …
Run Code Online (Sandbox Code Playgroud) 我有一个休息API来测试,我必须比较两个json响应.您可以在下面找到该文件的结构.要比较的两个文件应包含相同的元素,但顺序可能不同.不幸的是,名称,类型(简单,数组)和键数(root,nodeXYZ)也是未知的.
{"root": [{
"node1": "value1",
"node2": "value1",
"node3": [
{
"node311": "value311",
"node312": "value312"
},
{
"node321": "value321",
"node322": "value322"
}
],
"node4": [
{
"node411": "value411",
"node412": "value413",
"node413": [ {
"node4131": "value4131",
"node4132": "value4131"
}],
"node414": []
}
{
"node421": "value421",
"node422": "value422",
"node423": [ {
"node4231": "value4231",
"node4232": "value4231"
}],
"node424": []
}]
"node5": [
{"node51": "value51"},
{"node52": "value52"},
]
}]}
Run Code Online (Sandbox Code Playgroud)
我在Groovy中 找到了一些有用的信息 - 比较两个JSON对象(相同的结构)并返回包含差异的ArrayList 从Json Response Groovy 获取节点:如何使用键的值搜索json并在groovy中找到它的子节点 但我无法将它组合到解决方案.我认为解决方案可能如下所示:
take root
get root …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个用 Groovy 编写的小工具,用于解析电子邮件中的 JSON 字符串。其中一些 JSON 字符串具有包含转义引号的 JSON 值。
例如:
{
"hello": "world with \"quotation marks\""
}
Run Code Online (Sandbox Code Playgroud)
为了解析这些字符串,我使用了 Groovy 的 JsonSlurper。以下代码演示了我的问题:
import groovy.json.JsonException
import groovy.json.JsonSlurper
try {
print new JsonSlurper().parseText('''
{
"hello": "world with \"quotation marks\""
}
''')
} catch (JsonException | IllegalArgumentException e) {
print e
}
Run Code Online (Sandbox Code Playgroud)
有关实时演示,请参阅https://groovyconsole.appspot.com/script/6193189027315712。
执行此代码时,抛出以下异常:
Run Code Online (Sandbox Code Playgroud)groovy.json.JsonException: expecting '}' or ',' but got current char 'q' with an int value of 113 The current character read is 'q' with an int value of 113 …
我在 Ready!Api 1.9.0 中使用 Groovy 脚本来解码 SOAP 响应中返回的 Base64 字符串,并将生成的 JSON 对象存储在 json 文件中。然后获取生成的文件并使用 JsonSlurper 解析它以获取 Map 对象。
需要迭代该对象,以便我可以找到一个键并断言其值。我无法弄清楚为什么找不到钥匙。如果我直接使用 map.get(key) 调用键,则会收到错误“没有此类属性”。如果我直接使用 map.get('key') 调用它,它会返回 null。我也尝试过Map.each{k -> log.info("${k}")}
返回“interface.java.util.Map”而不是预期的键列表。
//create file path
def respFile = "C:\\Users\\me\\Documents\\Temp\\response.json"
//set originaldata in response to var
def response1 = context.expand( '${Method#Response#declare namespace ns4=\'com/service/path/v4\'; declare namespace ns1=\'com/other/service/path/v4\'; //ns1:RequestResponse[1]/ns1:GetAsset[1]/ns1:Asset[1]/ns4:DR[1]/ns4:Sources[1]/ns4:Source[1]/ns4:OriginalData[1]}' )
//decode the data
byte[] decoded = response1.decodeBase64()
//create file using file path above if it doesnt exist
def rf = new File(respFile)
//write data to file NOTE …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 JsonSlurper 来输入来自不同文件的变量。但它在第二次执行时失败了。有人可以帮助我吗?
它从步骤失败 Jenkins_File_Path = readFile ( "${Local_Path_App}" + "/Jenkinsfile" )
Main.groovy
import groovy.json.JsonSlurper
Global_Settings_Path = readFile ("${Main_Local_Path}" + "/Config/GlobalSettings_Java.json" )
def jsonSlurper = new JsonSlurper()
def GlobalVariables = jsonSlurper.parseText(Global_Settings_Path)
Jenkins_File_Path = readFile ( "${Local_Path_App}" + "/Jenkinsfile" )
def jsonSlurper1 = new JsonSlurper()
def json = jsonSlurper1.parseText(Jenkins_File_Path)
Run Code Online (Sandbox Code Playgroud)
GlobalSettings_Java.json
{
"LOB": {
"SVN_Config_Path": "testpath",
"Local_Path_LOB": "test",
}
}
Run Code Online (Sandbox Code Playgroud)
詹金斯档案
{
"BUILD_INFO": {
"Build_Type" : "Maven",
}
}
Run Code Online (Sandbox Code Playgroud)
错误日志
java.io.NotSerializableException: org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) 处的 groovy.json.internal.LazyMap org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java) ) 在 org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56) 在 org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50) 在 org.jboss.marshalling. …
我有需要使用 Groovy 处理的 JSON。我非常确定 JSON 只有一个键,格式如下:
{ rootKey: [...] }
Run Code Online (Sandbox Code Playgroud)
其中rootKey
代表不同的值(例如“客户”、“商店”等)。
假设我使用了 JsonSlurper:
def map = jsonSlurper.parseText(myjson)
Run Code Online (Sandbox Code Playgroud)
如何获取 rootKey 字符串?
groovy ×7
jsonslurper ×7
json ×6
escaping ×1
grails ×1
parsing ×1
soapui ×1
string ×1
validation ×1