JsonObject in Java - java

Original Question: Resolved
I'm producing Json from the controller using JsonObject/JsonArray:
JSONObject headers = new JSONObject();
headers.put("ID", "");
headers.put("Organization Name", "");
headers.put("Submission Date", "");
headers.put("Status", "");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );
This produces JSON as:
"headers":{"Status":"","Submission Date":"","Organization Name":"","ID":""}
Instead I need to get the output as:
"headers":[ "ID", "Organization Name", "Submission Date", "Status" ]
Is that possible? Please advise. Please note I can have the JSON as a javascript variable as well if that's easier?
EDIT:
I need one more simple change in the JSON output. Here is my code:
JSONObject notifications = new JSONObject();
notifications.put("id", "1");
notifications.put("description", "Notification 1");
notifications.put("createdTimestamp", "2015-05-12T18:15:28.237Z");
notifications.put("startTimestamp", "2015-05-25T18:30:28.237Z");
notifications.put("endTimestamp", "2015-06-13T12:30:30.237Z");
notifications.put("active", "true");
This generates JSON output as:
{"data":{"id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"}}
Instead I want it to generate as:
{"data":["id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"]}

headers should be a JSONArray, not JSONObject. An object is a set of name-to-value mappings, an array is a linear collection.
JSONArray headers = new JSONObject();
headers.add("ID");
headers.add("Organization Name");
headers.add("Submission Date");
headers.add("Status");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );

organizationsHJ.put("headers", headers.keySet());

Related

Json full of "\"

I'm putting a json inside another like this:
JSONObject json = new JSONObject();
for(.....){
JSONObject v = new JSONObject();
v.put("key", "value");
v.put("key", "value");
v.put("key", "value");
json.put(i, v.toString());
i++;
}
The problem is that it is getting like this:
{"0":"{\"key\":\"value\",\"key\":\"value\",...}"}
Filled with "\", does anyone know how to withdraw or do the "right mode"?
cause you put string in json. change this:
from:
json.put(i, v.toString());
to:
json.put(i, v);

Generate 2d Json Array In Java

I'm trying to generate a 2d JSON array using JSON object in Java. I'm trying to generate the following JSON.
Java Code..
JSONObject root = new JSONObject();
JSONObject c0 = new JSONObject();
JSONObject c1 = new JSONObject();
JSONObject attachment = new JSONObject();
JSONObject payload = new JSONObject();
JSONObject buttons = new JSONObject();
root.put("recipient", c0);
root.put("message", c1);
c0.put("id", userId);
c1.put("message", attachment);
attachment.put("type", "template");
attachment.put("payload", payload);
payload.put("template_type", "button");
payload.put("text", "What do you want to do next");
payload.put("buttons", buttons);
buttons.put("type", "web_url");
buttons.put("url", "https://google.com");
buttons.put("title", "show website");
buttons.put("type", "postback");
buttons.put("title", "Hi There");
buttons.put("payload", "sample payload");
Expected JSON Output..
{
"recipient":{
"id":"USER_ID"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"What do you want to do next?",
"buttons":[
{
"type":"web_url",
"url":"https://google.com",
"title":"Show Website"
},
{
"type":"postback",
"title":"Start Chatting",
"payload":"Sample_PAYLOAD"
}
]
}
}
}
}
Current Output..
{
"recipient":{"
id":"988459377921053"
},
"message":{
"message":{"
payload":{
"buttons":{
"payload":"sample payload",
"type":"postback",
"title":"Hi There",
"url":"https://google.com"
},
"template_type":"button",
"text":"What do you want to do next"},
"type":"template"
}
}
}
I'm creating nested Json objects and adding them from the outer level to the inner level still the output JSON is not as expected. Can't understand where I'm going wrong.
Edit 1:
Tried changes mentioned by user #user1802604 but the JSON being generated is of the following format..
{
"recipient":{
"id":"988459377921053"
},
"message":{
"attachment":{
"payload":{
"buttons":[
{
"payload":"sample payload",
"type":"postback",
"title":"Hi There",
"url":"https://google.com"
},
{
"payload":"sample payload",
"type":"postback",
"title":"Hi There",
"url":"https://google.com"
}
],
"template_type":"button",
"text":"What do you want to do next"
},
"type":"template"
}
}
}
The API to which I'm sending the JSON is returning response code 400 with message "Bad Request". Is there a way to preserve the order of elements??
I think most of your codes are right. Only two errors need to be fixed.
Note that I'm not sure what library you're using for representing json, so the following codes may not fully correct. I assume that you're using org.json.
c1.put("message", attachment); should be c1.put("attachment", attachment);.
JSONObject buttons = new JSONObject(); should be created as JSONArray buttons = new JSONArray();. So you also have to create another json object for storing type, title, and url.
JSONArray buttons = new JSONArray();
JSONObject button1 = new JSONObject();
button1.put("type", "web_url");
button1.put("url", "https://google.com");
button1.put("title", "show website");
buttons.put(button1);
JSONObject button2 = new JSONObject();
button2.put("payload", "postback");
button2.put("url", "Sample_PAYLOAD");
button2.put("title", "Start Chatting");
buttons.put(button2);
This code is settings value of a JSONObject
buttons.put("type", "web_url");
buttons.put("url", "https://google.com");
buttons.put("title", "show website");
buttons.put("type", "postback");
buttons.put("title", "Hi There");
buttons.put("payload", "sample payload");
Since there is twice each key, you override the first value.
What you want is to add a JSONObject instance into a JSONArray buttons.
JSONArray arrayButton= new JSONArray();
JSONObject button.put("type", "web_url");
button.put("url", "https://google.com");
button.put("title", "show website");
arrayButton.put(button);
button.put("type", "postback");
button.put("title", "Hi There");
button.put("payload", "sample payload");
arrayButton.put(button);
To add each object into an array. Then simply add the array you have build.

add array to json file with no key

I have to make a file in JSON format which must look like the following:
xyz.json
[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
The array should be added in the JSON file without {}, and these curly brackets have to come inside the JSON array.
The code for
{
instances:[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
}
is:
This code adds json array as a value to "instance" key, but I want to add json array without json key.
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
Please tell how does this code has to be changed in order to get the output asked above.
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
These two lines create the JSON object with curly braces. You don't need them, you can just remove them and use instances, which doesn't have curly braces as it's a json array and not a json object.
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
// not needed
//JsonObject launch_plan = new JsonObject();
//launch_plan.add("instances", instances);

Adding empty array to a JSON object

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());

Need to change my code to generate correct JSON object

I am trying to generate the following JSON to be accessed by tokenInput, but my code generates it in other way, how can I tailor the code to generate the correct JSON.
Required Json
get[{id:"set",name:"set"},{id:"object",name="object}]
or
[{id:"set",name:"set"},{id:"object",name="object}]
My JSON
{"get":[{"id":"set","name":"set"},{"id":"object","name":"object"}]}
MyCode
JSONObject res = new JSONObject();
JSONArray jarray = new JSONArray();
JSONObject object = new JSONObject();
object.put("id","set");
object.put("name", "set");
jarray.add(object);
JSONObject object1 = new JSONObject();
object1.put("id","object");
object1.put("name", "object");
jarray.add(object1);
res.put("get", jarray);
return res;
JSP
$(document).ready(function() {
$("#items").tokenInput("getOptions.jsp",{
theme: "facebook"
});
});
[{"id":"set","name":"set"},{"id":"object","name":"object"}]
this can be only form can get instead of form ur code generates . make res a JSONArray . then try .. !

Categories