I'm sending a json object to a REST url as :
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
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).
It appears my issue is with adding a name-value pair to JSONArray. Any help on how I can achieve this?
You have to create a JSONObject, put it in a JSONArray and then add it to your first JSONObject, try the following code:
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);
Logic would be the same for any library but the syntax will differ. I am using thecom.google.gson library.
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 :
JsonObject loan = new JsonObject();
loan.addProperty("clientId", "1");
loan.addProperty("productId", "1");
loan.addProperty("disbursementData", jsonArray.toString());
JSONArray implements Collection (the json.org implementation from which this API is derived does not have JSONArray implement Collection). And JSONObject has an overloaded put() method which takes a Collection and wraps it in a JSONArray (thus causing the problem). I think you need to force the other JSONObject.put() method to be used:
you try to add below line
loan.put("disbursementData", (Object)arr);
You should file a bug with the vendor, pretty sure their JSONObject.put(String,Collection) method is broken.
You are creating JSONArray, array is not map and doesnt support put(key,value).
You have to use put(int index, Object value), where index is the array index(same as array[index]).
In your case the value should be the JSONObject :
{
"expectedDisbursementDate":"21 December 2018",
"principal":2000,
"approvedPrincipal":2000
}
Related
I tried to build the nested array for the following json blob using jsonobject and jsonarray. For some reason, the object is failing. Any thoughts - please, how best we can put this format.
{
"reporter_desc": "pot",
"timestamp": "1487830751",
"incidents": [
{
"src_ip": 822382162,
"exploit": "scan",
"incident_type": 3,
"occurred": "1487830751",
"description": "pot scan"
}
Here is the JSONObject and JSONArray :
JSONObject object = new JSONObject();
object.put("reporter_desc", "mtpot");
object.put("timestamp", "1487830751");
JSONArray array = new JSONArray();
JSONObject arrayElement = new JSONObject();
arrayElement.put("src_ip", 822382162);
arrayElement.put("exploit", "scan");
arrayElement.put("incident_type", 3);
arrayElement.put("occurred", "1487830751");
arrayElement.put("description", "pot vulnerability scan");
array.put("incidents");
object.put("incidents", array);
I want to create a json array which looks like follows:
[{"label":"Organisation 1","ID":2},{"label":"Organisation 2","ID":1},....]
I did as follows :
JSONObject obj = new JSONObject();
while (rstcust.next()) {
obj.put("label", rstcust.getString(3));
obj.put("ID", rstcust.getInt(1));
}
out.print(obj);
I am getting only first value as output I am not getting like the fomat above:
I am getting the following output
{"label":"Organisation 1","ID":2}
This is happening because you are creating a JSONObject but what you need is a JSONArray
Create a JSONArray Object and then add individual JSONObjects into it.The code will be something like this.
JSONArray arr = new JSONArray();
while (rstcust.next()) {
JSONObject obj = new JSONObject();
obj.put("label", rstcust.getString(3));
obj.put("ID", rstcust.getInt(1));
arr.put(obj);
}
out.print(arr);
The JavaDocs for JSONArray can be found here.
In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
I am trying to create a JSON object that looks like the below :
{"filters":
{"defaultOperator":"OR",
"filters":[]}}
What should i add to my JSON object to make a empty array object ??. Cant get my head around an empty array .
tried jo1.put("filters",new Object[0]);. This did not work .
I am testing an API which requires these array filled only in special cases, most of the time its empty. But dont know how to add it. I am creating the entry JSON like
JSONObject jo1 = new JSONObject();
jo1.put("defaultOperator", "OR");
jo1.put("filters","[]");
jo3 = new JSONObject();
jo3.put("filters", jo1);
//Toast.makeText(getApplicationContext(),""+jo3.toString() , Toast.LENGTH_LONG).show();
//json = new GsonBuilder().create().toJson(comment1, Map.class);
httppost.setEntity(new StringEntity(jo3.toString()));
JSONArray jarr = new JSONArray();
jo1.put("filters",jarr);
or all on one line:
jo1.put("filters", new JSONArray());
I need to create in a Java (Android) a JSONObject/JSONArray with the exact following structure:
{
"listId":
[
"c02bc683-fcd7-47a5-b157-853e26ed099e",
"f8e1c9d7-ae45-4433-a315-726c1d912d09"
]
}
Can somebody help me on this please?
EDIT:
What i have is this:
JSONArray obj = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("listId", folderId);
obj.put(jsonObject);
JSONObject json = new JSONObject();
json.put("id", obj);
But this produces something like:
{
"listId":
[
{"id":"c02bc683-fcd7-47a5-b157-853e26ed099e"},
{"id":"f8e1c9d7-ae45-4433-a315-726c1d912d09"}
]
}
Thanks
You've got your array creation a bit mixed up. What you really want is an object holding an array of string, but what you're doing is creating an array of JSONObject.
Try this instead:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}
I'm using Gson and am trying to add a bunch of string values into a JsonArray like this:
JsonArray jArray = new JsonArray();
jArray.add("value1");
The problem is that the add method only takes a JsonElement.
I've tried to cast a String into a JsonElement but that didn't work.
How do I do it using Gson?
You can create a primitive that will contain the String value and add it to the array:
JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive("value1");
jArray.add(element);
Seems like you should make a new JsonPrimitive("value1") and add that.
See The javadoc
For newer versions of Gson library , now we can add Strings too. It has also extended support for adding Boolean, Character, Number etc. (see more here)
Using this works for me now:
JsonArray msisdnsArray = new JsonArray();
for (String msisdn : msisdns) {
msisdnsArray.add(msisdn);
}
I was hoping for something like this myself:
JsonObject jo = new JsonObject();
jo.addProperty("strings", new String[] { "value1", "value2" });
But unfortunately that isn't supported by GSON so I created this helper:
public static void Add(JsonObject jo, String property, String[] values) {
JsonArray array = new JsonArray();
for (String value : values) {
array.add(new JsonPrimitive(value));
}
jo.add(property, array);
}
And then use it like so:
JsonObject jo = new JsonObject();
Add(jo, "strings", new String[] { "value1", "value2" });
Voila!