我有一個包含許多 json 消息的 json 數組。然后我解析 json 消息以處理每個 json 對象。我需要弄清楚如何獲取一個失敗的 json 對象并將所有失敗的 json 對象附加回另一個 json 數組以創(chuàng)建一個新文件。我不確定如何將 json 對象從 parseText() 方法轉換回普通的 json 消息,或者如何將對象附加回 json 數組以創(chuàng)建文件。有人可以幫我弄這個嗎? 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" } ]
1 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
我找到了我的問題的答案。
JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonResp);
println jsonArray.toString()
添加回答
舉報
0/150
提交
取消