为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地8271018 2022-09-28 14:24:28
我正在使用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)        },
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

问题出在以下行中:

JSONObject responseobj = new JSONObject(obj.get("response").toString());

你把 是作为字符串到 JSON 对象,这就是为什么它以这种方式工作。


查看完整回答
反对 回复 2022-09-28
  • 1 回答
  • 0 关注
  • 260 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号