[英]How do I add a name-value pair to JSONArray
I'm sending a json object to a REST url as : 我正在将JSON对象发送到REST网址:
JSONObject loan=new JSONObject();
loan.put("clientId", "1");
loan.put("productId", "1");
Now I also have to send an array as part of the payload : 现在,我还必须发送一个数组作为有效负载的一部分:
{
"clientId": 1,
"productId": 1,
"disbursementData": [
{
"expectedDisbursementDate":"21 December 2018",
"principal":2000,
"approvedPrincipal":2000
}
]
}
How do I send the array disbursementData using the JSONObject as I'm doing with the other elements 与其他元素一样,如何使用JSONObject发送数组disbursementData
I have tried using: 我试过使用:
JSONArray arr = new JSONArray();
arr.put("expectedDisbursementDate","21 December 2018");
arr.put("principal", "1000");
arr.put("approvedPrincipal", "1000");
loan.put("disbursementData", arr);
I get the following exception : 我得到以下异常:
The method put(int, boolean) in the type JSONArray is not applicable for the arguments (String, String). JSONArray类型的方法put(int,boolean)不适用于参数(字符串,字符串)。
It appears my issue is with adding a name-value pair to JSONArray. 看来我的问题是将名称/值对添加到JSONArray。 Any help on how I can achieve this? 我如何实现此目标有帮助吗?
Logic would be the same for any library but the syntax will differ. 任何库的逻辑都是相同的,但是语法会有所不同。 I am using the com.google.gson
library. 我正在使用com.google.gson
库。
Create object to be placed in the array : 创建要放置在数组中的对象:
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty("expectedDisbursementDate", "21 December 2018");
jsonObj.addProperty("principal", "2000");
jsonObj.addProperty("approvedPrincipal", "2000");
Create the array and add the object to it : 创建数组并将对象添加到其中:
JsonArray jsonArray = new JsonArray();
jsonArray.add(jsonObj);
Add the array to the original json object : 将数组添加到原始json对象:
JsonObject loan = new JsonObject();
loan.addProperty("clientId", "1");
loan.addProperty("productId", "1");
loan.addProperty("disbursementData", jsonArray.toString());
You have to create a JSONObject, put it in a JSONArray and then add it to your first JSONObject, try the following code: 您必须创建一个JSONObject,将其放入JSONArray,然后将其添加到您的第一个JSONObject中,请尝试以下代码:
JSONObject aux=new JSONObject();
aux.put("expectedDisbursementDate","21 December 2018");
aux.put("principal", "1000");
aux.put("approvedPrincipal", "1000");
JSONArray arr = new JSONArray();
arr.put(aux);
loan.put("disbursementData",arr);
JSONArray implements Collection (the json.org implementation from which this API is derived does not have JSONArray implement Collection). JSONArray实现Collection(派生此API的json.org实现没有JSONArray实现Collection)。 And JSONObject has an overloaded put() method which takes a Collection and wraps it in a JSONArray (thus causing the problem). JSONObject有一个重载的put()方法,该方法接受一个Collection并将其包装在JSONArray中(因此导致问题)。 I think you need to force the other JSONObject.put() method to be used: 我认为您需要强制使用其他JSONObject.put()方法:
you try to add below line 您尝试添加以下行
loan.put("disbursementData", (Object)arr); loan.put(“ disbursementData”,(Object)arr);
You should file a bug with the vendor, pretty sure their JSONObject.put(String,Collection) method is broken. 您应该向供应商提出错误,非常确定他们的JSONObject.put(String,Collection)方法已损坏。
You are creating JSONArray, array is not map and doesnt support put(key,value). 您正在创建JSONArray,数组未映射,并且不支持put(key,value)。 You have to use put(int index, Object value), where index is the array index(same as array[index]). 您必须使用put(int index,Object value),其中index是数组索引(与array [index]相同)。 In your case the value should be the JSONObject : 在您的情况下,该值应为JSONObject:
{
"expectedDisbursementDate":"21 December 2018",
"principal":2000,
"approvedPrincipal":2000
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.