【发布时间】:2018-10-06 05:29:51
【问题描述】:
我有一个包含许多 json 消息的 json 数组。然后我解析 json 消息以处理每个 json 对象。 我需要弄清楚如何获取一个失败的 json 对象并将所有失败的 json 对象附加回来 进入另一个 json 数组以创建一个新文件。我不确定如何从 parseText() 方法转换 json 对象 回到正常的 json 消息或如何将对象附加回 json 数组以创建文件。有人可以帮我解决这个问题吗?
Main json file array
[
{
"Account": "1",
"Name": "Test1"
},
{
"Account": "2",
"Name": "Test2"
},
{
"Account": "3",
"Name": "Test3"
},
{
"Account": "4",
"Name": "Test4"
}
]
String sJson = groovy.json.StringEscapeUtils.unescapeJava(jsonFile.toString());
jsonResp = new groovy.json.JsonSlurper().parseText(sJson));
for( int x=0; x < jsonResp?.size(); x++ ) {
processJson( jsonResp[x] )
}
void processJson( Object jsonResp ) {
If object message fails in this function, need to convert the json object back into a json message
and append it to a jsonArray to create a new json file.
}
This is what the new json file would like if test samples 2 & 4 failed
[
{
"Account": "2",
"Name": "Test2"
},
{
"Account": "4",
"Name": "Test4"
}
]
【问题讨论】: