从一个文本文件中提取两个不同的JSON字符串到一个JSON数组中。

问题描述 投票:0回答:1

我试图从一个文本文件中读取两个不同的JSON对象到一个JSON数组中,但我只能提取第一个字符串,而不能提取第二个。

    class CreateFinalResponse {
    public static void main(String[] args) {

        String fileContents = new File('C:\\Users\\Input\\intermediateResponse.txt').getText('UTF-8')
        println(fileContents);
        println("*************************************************")
        def inputJSON = new JsonSlurper().parseText(fileContents)
        inputJSON.each{ println it }
    }
}

对于上面的代码,我得到了以下的响应。

    {
    "employeeId": {
        "0": "10999999",
        "1": "10999999",
        "2": "10999999",
        "3": "10999999",
        "4": "10999999",
        "5": "10999999",
        "6": "10999999",
        "7": "10999999",
        "8": "10999999",
        "9": "10999999",
        "10": "10999999",
        "11": "10999999",
        "12": "10999999",
        "13": "10999999",
        "14": "10999999",
        "15": "10999999",
        "16": "10999999",
        "17": "10999999",
        "18": "10999999",
        "19": "10999999"
    }
}

    {
        "response": {
            "ExecuteResult": "false",
            "outputvalue1": "311",
            "outputvalue2": "8fc3b98c342b9898c4adde51ca8b951c0de1b53930d5512a9c410212bb3a1f956fb51fe4f3c2355951ee79f4bab6eda39a71937f6983315be4dd334777135584",
            "outputvalue3": "Partial Upload successful, a few employee's failed",
            "message": "[Response :0:500:ex\":0,\"httpCode\":200,\"inlineResults\":null}]]"
        }
    }
    *************************************************
    employeeId={0=10999999, 1=10999999, 2=10999999, 3=10999999, 4=10999999, 5=10999999, 6=10999999, 7=10999999, 8=10999999, 9=10999999, 10=10999999, 11=10999999, 12=10999999, 13=10999999, 14=10999999, 15=10999999, 16=10999999, 17=10999999, 18=10999999, 19=10999999}

我如何将这两个JSON提取到一个数组中?

json groovy
1个回答
1
投票
String fileContents = new File('C:\\Users\\Input\\intermediateResponse.txt').getText('UTF-8')    
def splited = fileContents.split("\n\n")
def listOfJSONs = []
splited.each{
    listOfJSONs << new JsonSlurper().parseText(it)
}
listOfJSONs.each{ println it }
© www.soinside.com 2019 - 2024. All rights reserved.