简体   繁体   English

如何在请求正文中发送嵌套的json对象

[英]How to send nested json objects inside a request body

how can i send this request in the body of my volley request, thanks in advance 我如何在排球请求的正文中发送此请求,请提前感谢

{
  "amount": "000",
  "card": {
    "number": "00000",
    "expiry_month": "00",
    "expiry_year": "00",
    "cvv": "000",
    "pin": "000"
  }
}

this is my request parameters, i have try this the api is tell me invalid parametes, please guzs help me 这是我的请求参数,我已经尝试过了,这个api告诉我无效的参数,请guzs帮助我

 @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", User_Token);
            headers.put("Content-Type", "application/json");
            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }



        @Override
        public byte[] getBody() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "amount", amount);
            params.put( "card", String.valueOf(come()));

            return new JSONObject(params).toString().getBytes();
        }
        private byte[] come() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "number", number);
            params.put( "expiry_month", month);
            params.put( "expiry_year", year);
            params.put( "cvv", cvv);
            params.put( "pin", pin);
            return new JSONObject(params).toString().getBytes();
        }

Use JsonObject instead of: creating a Map , converting it to Byte[] and then getting its String.valueOf(...) . 使用JsonObject代替:创建Map ,将其转换为Byte[] ,然后获取其String.valueOf(...) This will allow you to send your complete object as body of the request, instead of the incomplete body that is being sent right now: {"amount":"000","card":"[B@a2e...."} ) 这将使您可以将完整的对象作为请求的正文发送,而不是立即发送的不完整的正文: {"amount":"000","card":"[B@a2e...."}

The problem with the value that is being sent for "card" : "[B@a2e...." is that it's not a representation of your object with its properties. 正在为"card"发送的值存在问题: "[B@a2e...."是,它不代表对象及其属性。 Instead, it is only the memory address of the Byte Array you created. 而是仅是您创建的字节数组的内存地址。

In your code, use JsonObject for your objects and only do the conversion to Byte[] at the end of getBody() method, since that is the place where you finally return your complete object: 在您的代码中,将JsonObject用于对象,并仅在getBody()方法末尾执行到Byte[]的转换,因为这是您最终返回完整对象的地方:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", User_Token);
    // headers.put("Content-Type", "application/json");  // This is probably redundant, as you are already setting it on getBodyContentType()
    return headers;
}

@Override
public String getBodyContentType() {
    return "application/json";
}

@Override
public byte[] getBody() {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("amount", amount);
        jsonObject.put("card", come());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject.toString().getBytes();
}

private JSONObject come() throws JSONException {
    JSONObject params = new JSONObject();
    params.put( "number", number);
    params.put( "expiry_month", month);
    params.put( "expiry_year", year);
    params.put( "cvv", cvv);
    params.put( "pin", pin);
    return params;
}

In case you need exact format you need to make sure that values of number , month etc, are passed as strings but not integers. 如果需要精确的格式,则需要确保将numbermonth等值作为字符串而不是整数传递。 Also to make code look shorter and less destructive you could chain put calls. 同样,为了使代码看起来更短,破坏性更小,您可以链接put调用。 Here is a bit refactored version: 这是一个重构版本:

@Override
public byte[] getBody() {  
    try {
        final JSONObject card = new JSONObject()
                .put("number", String.valueOf(number))
                .put("expiry_month", String.valueOf(month))
                .put("expiry_year", String.valueOf(year))
                .put("cvv", String.valueOf(cvv))
                .put("pin", String.valueOf(pin));

        return new JSONObject()
                .put("amount", String.valueOf(amount))
                .put("card", card)
                .toString()
                .getBytes();    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Empty json return will not help us as in this case 
    //we will send useless empty body to the server. 
    return null;
}

I don't know what on caller side of getBody() is happening and whether or not you are able to modify that code. 我不知道getBody()调用getBody()什么,以及您是否能够修改该代码。 But you need somehow to track that null return or instead introduce your own exception class that should be checked on getBody() caller side. 但是您需要某种方式来跟踪该null返回值,或者引入您自己的异常类,该异常类应在getBody()调用方进行检查。 But in case you have no control over getBody() caller side you need return some non null , empty return: 但是,如果您无法控制getBody()调用方,则需要返回一些非null空返回值:

    return new byte[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM