使用 apache Velocity 进行 JSON 到 JSON 转换?有没有更好的方法来编写我在

我正在使用apache速度进行json转换。下面是我的 json 字符串


    {

    "apiCode": "Payment Execution Service",

    "name": "Initiate a payment",

    "description": "Initiate a payment",

    "request": {

        "method": "POST",

        "path": "/api/v1/payments",

        "headers": [

            {

                "Corporate-ID": "apiKey"

            },

            {

                "Content-Type": "application/json"

            }

        ],

        "body": "{\n  \"beneficiaryInformation\" : {\n    \"destinationAccountIdentifier\" : \"string\",\n    \"destinationBankIdentifier\" : \"DEUTDEDB237\",\n    \"fullName\" : \"JASON SMITH\"\n  },\n  \"purposeOfPayment\" : \"Invoice Payment\",\n  \"remittanceInformation\" : \"Mantainance of Fixtures\",\n  \"remitterInformation\" : {\n    \"sourceAccountCurrency\" : \"EUR\",\n    \"sourceAccountIdentifier\" : \"string\",\n    \"sourceBankIdentifier\" : \"DEUTDEDBFRA\"\n  },\n  \"transferAmount\" : 1.5,\n  \"transferCurrency\" : \"EUR\",\n  \"transferDate\" : \"2015-07-20\",\n  \"transferType\" : \"SCTInst\",\n  \"uniqueRequestNo\" : \"string\"\n}"

    },

    "response": {

        "status": 200,

        "headers": [

            {

                "Content-Type": "application/json"

            }

        ],

        "body": "{\n  \"requestReferenceNo\" : \"string\",\n  \"transactionStatus\" : {\n    \"bankReferenceNo\" : \"string\",\n    \"reasonCode\" : \"string\",\n    \"statusCode\" : \"string\"\n  }\n}"

    },

    "provider": "Payment Execution Service"

}

下面是我的 .vm 文件


   {

"provider": {

    "name": "$arr[0].apiCode"

},

"consumer": {

    "name": "$arr[0].provider"

},

"interactions": [

#set($i = 0)

#foreach($a in $arr) 

{

        "description": "$a.description",

        "request": {

            "path": "$a.request.path",

            "method": "$a.request.method",

            "headers": $json.valueToString($a.request.headers),

            "body": $json.valueToString($a.request.body)

        },


慕的地8271018
浏览 261回答 1
1回答

ITMISS

问题出在以下行中:JSONObject responseobj = new JSONObject(obj.get("response").toString());你把 是作为字符串到 JSON 对象,这就是为什么它以这种方式工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java