gpt4 book ai didi

java - Eclipse 上的 Groovy 错误

转载 作者:行者123 更新时间:2023-12-01 13:30:03 25 4
gpt4 key购买 nike

对于上面的代码,它出现错误

“if(recJsonObj instanceof JSONObject){”行我对 groovy.code 完全陌生,如下所示..可能是什么错误???我对这种语言真的很沮丧。帮助我..

   import org.json.*;
class Convert {
def JSONDifference(JSONObject recJsonObj, JSONObject expJsonObj){
//File logFile = new File("C:\\TestCases\\my1.txt")
def pass = true
def recArrStrElements = recJsonObj.getNames(recJsonObj)
def expArrStrElements = expJsonObj.getNames(expJsonObj)
def found = false
def foundMacthForCurrentElement = false

for (int x = 0; x < recArrStrElements.size(); x++) {
def recCurrVal = recJsonObj.get(recArrStrElements[x].toString())
foundMacthForCurrentElement = false

for (int y = 0; y < expArrStrElements.size(); y++) {
def expCurrVal = expJsonObj.get(expArrStrElements[y].toString())
if(recArrStrElements[x] == expArrStrElements[y]){
if(recCurrVal instanceof JSONArray){
recCurrValJsonArray = (JSONArray)recCurrVal
expCurrValJsonArray = (JSONArray)expCurrVal

for(int i = 0; i<recCurrValJsonArray.length(); i++){
recNestedJsonObj = recCurrValJsonArray.get(i)
//for(int j = 0; j<expCurrValJsonArray.length(); j++){
//if(foundMacthForCurrentElement){break}
//else{
expNestedJsonObj = expCurrValJsonArray.get(i)
if(recNestedJsonObj instanceof JSONObject){
foundMatch = JSONDifference(recNestedJsonObj, expNestedJsonObj)
}
else{

}
// if(foundMatch){
// foundMacthForCurrentElement = true
// }
//}
//}

}

found=true
}

else if(recCurrVal instanceof JSONObject){
recCurrValJSONObject = (JSONObject)recCurrVal
expCurrValJSONObject = (JSONObject)expCurrVal
JSONDifference(recCurrValJSONObject, expCurrValJSONObject)
found=true
}

else{
if(recCurrVal == expCurrVal){
//log.info "Element matched " + recCurrVal + ":" + recArrStrElements[x]
//logFile.append("Matched "+recArrStrElements[x])
//logFile.append("\n")
found=true
}
else{
found=false
}
}
}
}

if (!found){
pass = false
log.info "Element not received " + recArrStrElements[x] + ":"+ recCurrVal + " ====/==== " + expJsonObj.get(expArrStrElements[x].toString())
//logFile.append("Element not received " + recArrStrElements[x] + ":" + recCurrVal)
//logFile.append("\n")
}

//logFile.append(arrStrElements[x].toString())
//logFile.append("-")
//logFile.append(currVal.toString())
//logFile.append("\n")

//if(currVal.contains("{")){
// JSONObject nestedJsonObj = new JSONObject(arrStrElements[x].toString)
// JSONDifference(nestedJsonObj)
//}
}

return pass
}
if(recJsonObj instanceof JSONObject){
JSONObject jsonObjRec = new JSONObject(expJSONResp)
JSONObject jsonObjExp = new JSONObject(resJSONResp)
temp = JSONDifference(jsonObjRec, jsonObjExp)
}
else{
expSplitString = expJSONResp.split("\n")
resSplitString = resJSONResp.split("\n")

for (int a = 0; a < expSplitString.size(); a++) {
foundMacthForCurrentElement = false
int b = 0
while (b < resSplitString.size() && !foundMacthForCurrentElement) {
//log.info expSplitString[a] + " |||| " + resSplitString[b]
if (expSplitString[a].replaceAll("\\s+","")==resSplitString[b].replaceAll("\\s+","")){
foundMacthForCurrentElement = true
}
b++
}
if(!foundMacthForCurrentElement){
log.info "Element not received :- " + expSplitString[a].replaceAll("\\s+","") + " ====/==== " + resSplitString[a].replaceAll("\\s+","")
}
}
}

}

最佳答案

看起来您在类中的方法之外编写了一些代码。您应该将该代码放入某个方法中:

if(recJsonObj instanceof JSONObject){
JSONObject jsonObjRec = new JSONObject(expJSONResp)
JSONObject jsonObjExp = new JSONObject(resJSONResp)
temp = JSONDifference(jsonObjRec, jsonObjExp)
}
else{
expSplitString = expJSONResp.split("\n")
resSplitString = resJSONResp.split("\n")

for (int a = 0; a < expSplitString.size(); a++) {
foundMacthForCurrentElement = false
int b = 0
while (b < resSplitString.size() && !foundMacthForCurrentElement) {
//log.info expSplitString[a] + " |||| " + resSplitString[b]
if (expSplitString[a].replaceAll("\\s+","")==resSplitString[b].replaceAll("\\s+","")){
foundMacthForCurrentElement = true
}
b++
}
if(!foundMacthForCurrentElement){
log.info "Element not received :- " + expSplitString[a].replaceAll("\\s+","") + " ====/==== " + resSplitString[a].replaceAll("\\s+","")
}
}
}

关于java - Eclipse 上的 Groovy 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21622462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com