如何从 JSON 中删除括号?
How to remove brackets from JSON?
我有一个 groovy 脚本,我想 return userDefinedErrorText。我遇到的问题是,在解析我的 JSON 时,我的 failedForm 变量等于 [Failed] 而不是“Failed”。
如果我从 JSON 输入中删除第一对 [],我会得到正确的“失败”。
有没有办法从输入 JSON 中删除 []?
我的Groovy
def json = new JsonSlurper().parseText( aInputJson )
failedForm = json?.userDefinedErrorText
if( failedForm == "Failed" ) {
resultMessage = "false"
}
JSON
[
{
"step": "abcd",
"message": {
"ServiceRequest: abc": {
"App Stack Form Exception": {
"Expecting Form": "P",
"Resulting Form": "P"
},
"JAS Response": {
"fs_P": {
"title": "I",
"data": {},
"errors": [
{
"CODE": "799L",
"TITLE": "Error: Invalid Long Address Number",
"ERRORCONTROL": "15",
"DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\u000a Master file (F0101).\u000aRESOLUTION. . Enter a valid long address number.",
"MOBILE": "The long address number entered is not found in the Address Book\u000a Master file (F0101)."
}
],
"warnings": []
},
"stackId": 12,
"stateId": 5,
"rid": "8f4",
"currentApp": "P",
"timeStamp": "2022-04-22:11.25.03",
"sysErrors": []
}
}
},
"timeStamp": "2022-04-22T11:25:03.235-0400",
"userDefinedErrorText": "Failed"
}
]
The issue I am having is that when parse my JSON I am having my
failedForm variable equal [Failed] instead of "Failed".
以下应该有效:
String jsonString = '''
[
{
"step": "abcd",
"message": {
"ServiceRequest: abc": {
"App Stack Form Exception": {
"Expecting Form": "P",
"Resulting Form": "P"
},
"JAS Response": {
"fs_P": {
"title": "I",
"data": {},
"errors": [
{
"CODE": "799L",
"TITLE": "Error: Invalid Long Address Number",
"ERRORCONTROL": "15",
"DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\\u000a Master file (F0101).\\u000aRESOLUTION. . Enter a valid long address number.",
"MOBILE": "The long address number entered is not found in the Address Book\\u000a Master file (F0101)."
}
],
"warnings": []
},
"stackId": 12,
"stateId": 5,
"rid": "8f4",
"currentApp": "P",
"timeStamp": "2022-04-22:11.25.03",
"sysErrors": []
}
}
},
"timeStamp": "2022-04-22T11:25:03.235-0400",
"userDefinedErrorText": "Failed"
}
]'''
def json = new JsonSlurper().parseText(jsonString)
String value = json[0].userDefinedErrorText
assert value == 'Failed'
我有一个 groovy 脚本,我想 return userDefinedErrorText。我遇到的问题是,在解析我的 JSON 时,我的 failedForm 变量等于 [Failed] 而不是“Failed”。
如果我从 JSON 输入中删除第一对 [],我会得到正确的“失败”。
有没有办法从输入 JSON 中删除 []?
我的Groovy
def json = new JsonSlurper().parseText( aInputJson )
failedForm = json?.userDefinedErrorText
if( failedForm == "Failed" ) {
resultMessage = "false"
}
JSON
[
{
"step": "abcd",
"message": {
"ServiceRequest: abc": {
"App Stack Form Exception": {
"Expecting Form": "P",
"Resulting Form": "P"
},
"JAS Response": {
"fs_P": {
"title": "I",
"data": {},
"errors": [
{
"CODE": "799L",
"TITLE": "Error: Invalid Long Address Number",
"ERRORCONTROL": "15",
"DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\u000a Master file (F0101).\u000aRESOLUTION. . Enter a valid long address number.",
"MOBILE": "The long address number entered is not found in the Address Book\u000a Master file (F0101)."
}
],
"warnings": []
},
"stackId": 12,
"stateId": 5,
"rid": "8f4",
"currentApp": "P",
"timeStamp": "2022-04-22:11.25.03",
"sysErrors": []
}
}
},
"timeStamp": "2022-04-22T11:25:03.235-0400",
"userDefinedErrorText": "Failed"
}
]
The issue I am having is that when parse my JSON I am having my failedForm variable equal [Failed] instead of "Failed".
以下应该有效:
String jsonString = '''
[
{
"step": "abcd",
"message": {
"ServiceRequest: abc": {
"App Stack Form Exception": {
"Expecting Form": "P",
"Resulting Form": "P"
},
"JAS Response": {
"fs_P": {
"title": "I",
"data": {},
"errors": [
{
"CODE": "799L",
"TITLE": "Error: Invalid Long Address Number",
"ERRORCONTROL": "15",
"DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\\u000a Master file (F0101).\\u000aRESOLUTION. . Enter a valid long address number.",
"MOBILE": "The long address number entered is not found in the Address Book\\u000a Master file (F0101)."
}
],
"warnings": []
},
"stackId": 12,
"stateId": 5,
"rid": "8f4",
"currentApp": "P",
"timeStamp": "2022-04-22:11.25.03",
"sysErrors": []
}
}
},
"timeStamp": "2022-04-22T11:25:03.235-0400",
"userDefinedErrorText": "Failed"
}
]'''
def json = new JsonSlurper().parseText(jsonString)
String value = json[0].userDefinedErrorText
assert value == 'Failed'