java - Eclipse 上的 Groovy 错误

标签 java groovy

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

“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/

相关文章:

java - 在 cucumber 的@Given @When @Then之间传递变量

java - 如何使用 SharedPreferences 保存 SparseBooleanArray?或者还有其他选择吗?

java - Groovy/Java : Parallel processing of directory structure where each node is a list of subdirectories/files

spring - 多个 Camel 路线处理同一个文件

java - 如何使用 Java NIO 从套接字高效读取

java - 服务器上的 youtube 到 mp3 转换

java - 这个程序从哪里获取它的数字,为什么这是由于增加 1 数组大小引起的? ( java )

groovy - 如何在每个触发器的基础上更改 Jenkins email-ext 插件中的 FROM 电子邮件地址?

linux - 在gradle中维护属性序列

java - 从Gradle访问PATH变量?