[英]Groovy Error on eclipse
對於上面的代碼,它出現錯誤
“ 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+","")
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.