【问题标题】:Convert JSON Objects from JsonSlurper back to an appended JSONArray - Groovy将 JSON 对象从 JsonSlurper 转换回附加的 JSONArray - Groovy
【发布时间】: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"
      }
    ]  

【问题讨论】:

    标签: java json groovy


    【解决方案1】:

    我找到了问题的答案。

    JSONArray jsonArray = new JSONArray();
    jsonArray.add(jsonResp);
    println jsonArray.toString()
    

    【讨论】:

    • 什么是 JSONArray 的全限定类名
    • 导入 org.json.simple.JSONArray;
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2013-11-15
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多