首页 > 解决方案 > okHTTP POST 请求正文有很多孩子

问题描述

我正在使用运营商的收费 API,API 调用必须传递以下 JSON 格式。我正在使用 okHTTP 库。

String telNum = "+941234567";

String pbody = "{\"amountTransaction\": {\"clientCorrelator\": \"7659\",\"endUserId\": \"tel:"+telNum+"\",\"paymentAmount\": {\"chargingInformation\": {\"amount\": 1,\"currency\": \"LKR\",\"description\": \"Test Charge\"},\"chargingMetaData\": {\"onBehalfOf\": \"IdeaBiz Test\",\"purchaseCategoryCode\": \"Service\",\"channel\": \"WAP\",\"taxAmount\": \"0\",\"serviceID\": \"theserviceid\"}},\"referenceCode\": \"REF-12345\",\"transactionOperationStatus\": \"Charged\"}}";```

以下是 JSON 需要如何格式化。

{
    "amountTransaction": {
        "clientCorrelator": "54321",
        "endUserId": "tel:+94761234567",
        "paymentAmount": {
            "chargingInformation": {
                "amount": 1,
                "currency": "LKR",
                "description": "Test Charge"
            },
            "chargingMetaData": {
                "onBehalfOf": "IdeaBiz Test",
                "purchaseCategoryCode": "Service",
                "channel": "WAP",
                "taxAmount": "0",
                "serviceID": "null"
            }  
        },
        "referenceCode": "REF-12345",
        "transactionOperationStatus": "Charged"
    }
}

我收到错误 400 错误请求

标签: javajsonokhttp

解决方案


试试这个会根据你的需要格式化你的身体

 try {
        JSONObject jsonObject = new JSONObject(pbody);
        pbody=jsonObject.toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }

推荐阅读