I've a server when it receives a message like abc/xyz=yo then what I want is to check the existing json file and if this already exists then it will update the file with new value(just for the info. / means creating nested objects and value after = is the value we want to put in json.
I can easily create json if there isn't any value but the problem comes when I try to check if the json object contains that. I can use if(object.has(..)) in a for loop but I can not edit the nested object, like
//abc/xyz would be spited into String[] children = string.split("/"))
JSONObject object = new JSONObject(myJSONString);
JSONObject tmpObjext = object.getJSONObject("root")
for(int i=0;i=children.length;i++){
if(object.has(children[i]){
tmpObject = tmpObject.getJSONObject(children[i[)
/*let the initial json be like
{"root":{"abc":{"xyz"="yo"}}}
so the above code will return the final object i.e. {"xyz":"yo"}
so I tried getting rest of the children i.e. "abc"
and create a new json with updated value but as it
would be user generated I don't know how many
times it would be nested (in this case it's nested
twice once to add it to root and once to acc xyz to abc)
so I'll again have {"abc":{"xyz":"yo"}} so that I
can put it in main object like object.put("root",newJSON) but this would create
problem if there were multiple objects in it like
{"abc":{"xyz":"yo"}{"pqr":"yoi"}} so if I just
changed the xyz value and regenerate json and put
in root it will just put updated value of xyz and
remove the pqr field.
*/
}
}
I figured out how we can do this. you just need to create the desired object then again create another JSONObject and equate it with original one like
JSONObject o1 = new JSONObject(yourJSON);
JSONObject o2 = o1.getJSONObject("yourRootIfAny");
then you can use o2 to query things like
for (your children){
if(o2.has(child)){
o2 = o2.getJSONObject(child)
}
}
for more details you can check this out.
I am using the following libraries to create some JSON object.
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
The json I am trying to create is this:
{
"function": "create_contact_group",
"parameters": [{
"user_id": "teer",
"comp_id": "97",
"contact_group_name": "Test01",
"invite_user_list": [{
"invite_user_id": "steve"
}]
}]
}
My function looks like this:
public JSONObject createJSONRequest() {
/* Create json object */
JSONObject jsonObject = new JSONObject();
Map<String, String> map = new HashMap<>();
map.put("user_id", "teer");
map.put("comp_id", "97");
map.put("contact_group_name", "Test01");
List<String> mInviteUserList = new ArrayList<>();
mInviteUserList.add("steve");
/* Create the list of invitee */
Map<String, String> inviteList = new HashMap<>();
for(String user : mInviteUserList) {
inviteList.put("invite_user_id", user);
}
/* Add the invitees into the json array */
JSONArray inviteArray = new JSONArray();
inviteArray.add(inviteList);
/* Add the json array to the json object */
jsonObject.put("invite_user_list", inviteArray);
JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);
jsonObject.put("parameters", parameterlist);
jsonObject.put("function", "create_contact_group");
Log.d(TAG, "jsonObject: " + jsonObject.toJSONString());
return jsonObject;
}
However, the function crashes when I get to the following line:
Log.d(TAG, "jsonObject: " + jsonObject.toJSONString())
I think it has something to do with this line here:
parameterlist.add(jsonObject);
Stacktrace:
java.lang.StackOverflowError java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95) at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
at java.lang.StringBuffer.append(StringBuffer.java:126) at org.json.simple.JSONValue.escape(JSONValue.java:266) at org.json.simple.JSONObject.toJSONString(JSONObject.java:116)
Many thanks for any suggestions,
There are a couple of issues in your code.
One issue is that the structure you create in the java code will not match the structure that you show above. This I will try to describe a bit below.
The second issue is that you get a stackoverflow exception (which you know but don't know why).
The stackoverflow exception is thrown cause the program runs out of the stack memory assigned by the computer. Why you ask? Well, cause you create a recursive or cyclic JSON object.
This isn't good, but its not that big a deal cause its kinda easy to fix.
So why does the program throw this exception? Well, look at the following snippet:
JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);
jsonObject.put("parameters", parameterlist);
jsonObject.put("function", "create_contact_group");
You create a JSONArray and then add the JSONObject created before to the array.
After that you add the same array to the object that is already in the array.
I expect that you see the issue with that!
So, that should not be done.
And how to fix this? Well, I kinda think its better that I describe how you should write the code to get the structure you are actually asking for, so I'll try do that...
What to do...?
A JSON (JavaScript Object Notation) -object is always declared with this type of brackets: {} a JSON array with [], so, the JSON you are trying to generate should be in the following data types:
{ // <= Root object (a JSON-object).
"function": "create_contact_group", // <= Key in the root-object (where the key is a string and the value a string)
"parameters": [ // <= Key in the root-object (where the key is a string and the value is an array.
{ // <= Object inside the array.
"user_id": "teer", // Key (string/string)
"comp_id": "97", // Key (string/string)
"contact_group_name": "Test01", // Key (string/string)
"invite_user_list": [ // Key (string/array)
{ // Object inside the invite_user_list array
"invite_user_id": "steve" // Key (string/string)
}
]
}
]
}
So when creating the JSON-object in java, you will want to create a root object then add all the diff params inside it.
Adding a value to a JSONObject is done with the JSONObject.put(string, Object) method, where the string is a key and the object a value.
So to start, I would recommend creating the parameters list.
In your case, you use a HashMap for the objects, which is not really wrong, but not really necessary either, I would just stick to a JSONObject, which is not all that different than a HashMap<string, Object>.
So instead of map.put(...), you could do something like:
JSONObject param = new JSONObject();
param.put("user_id", "teer");
param.put("comp_id", "97");
param.put("contact_group_name", "Test01");
Now, one of the objects values should be an array (invite_user_id) and the easiest way to add an array to the object is to create a JSONArray and then just add it.
JSONArray inviteList = new JSONArray();
// Then you need to add an object to the array for each `user` that has invited.
// For-loop here maybe?
JSONObject invitee = new JSONObject();
invitee.put("invite_user_id", user);
inviteList,add(invitee); // This will make it into an array with objects, I.E., [ { "invite_user_id": "Steve" } ]
After creating the invite list, add it to the param object like:
param.put("invite_user_list", inviteList);
// Now, param should be in its own list too, so you should probably create a JSONArray for the params.
// Ill leave that to you, and we pretend we have a list of the param objects named "params".
And then at the end, you create the root object and set its values:
JSONObject root = new JSONObject();
root.put("parameters", params);
root.put("function", "create_contact_group");
And that should be it.
This should create a JSON-string with the structure that you made above. But I would recommend testing (and writing unit tests!) for this (especially as I have written this code in the browser!).
But why?!
I guess I should try to describe why your code was not working as the one I described above.
You start by creating a root object, so far so good (can create it at start or at the time you need it, doesn't really matter), after that you create a HashMap and add the properties to it.
So far this is also legit (you could later create a JSONObject from the map).
In the next part, you create an ArrayList (im not really sure why) and add a name to it, and then another HashMap which you add the single name to (key invite_user_list) inside a for-loop.
This is either not necessary (cause its just one name) or wrong (if there is supposed to be more names in a real life execution of the code), in case of unnecessary, the for-loop shouldn't be there and in case of "not like real life" it should not be added to a Map!
Instead the invieList should have been an array, and each entry added should have been a object which had the "invite_user_id" key set to the name.
After that, you add the inviteList HashMap to a newly created JSONArray, I guess this could be kinda okay, if you only want one object ever in the array, else I would recommend creating it before the loop and add each entry into it!
The inviteArray is then put inside the root object with the key invite_user_list, after that you create another JSONArray and add both the map (your parameters created at the start) and the JSONObject (root) created first of all.
But the thing you do after that, is why you are getting a stackoverflow exception, you add the parameterlist (which contains the jsonObject (root)) to the jsonObject, which makes the jsonObject exist inside an array that is inside itself!
This creates a cyclic JSON structure which will never end if the whole thing was to be unrolled, hence the computer throws the exception.
The structure of the resulting object would also be wrong, cause it would look something like this:
{ // Root (jsonObject)
"invite_user_list": [
{ "invite_user_id": "steve" }
]
"parameters": [
{ // The "map" hashmap
"user_id", "teer",
"comp_id": "97",
"contact_group_name": "Test01"
},
{ // The jsonObject object (which is also the root!)
"invite_user_list": [
{ "invite_user_id": "steve" }
],
"parameters": [
{ // The "map" hashmap
"user_id", "teer",
"comp_id": "97",
"contact_group_name": "Test01"
},
{
// The jsonObject object again (which is also the root and the parent!)
// ... and so on til eternity!
}
],
"function": "create_contact_group"
}
],
"function": "create_contact_group"
}
Extra...
I would like to add here at the end (where I hope you end up after reading the whole wall of text that I wrote above, cause you might have learnt something!) that there is a easier way of doing it.
Now, I haven't used this lib myself, but from what I understand, it should be able to serialize a whole object, the lib can be found at Googles github repos which can be used as a json serializer and convert a class-instance to a json string, then you could just create a class for the whole object and fill it up and serialize it at the end of the function, without using either JSONArray nor JSONObject.
The issue is due to recursion process that occurs when you are trying to add the JsonObject to JsonArray and viceVersa.
The thing you are doing is,
JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);
And then
jsonObject.put("parameters", parameterlist);
The problem is when you print the object using jsonObject.toJSONString(), Then at first it will fetch the parameterlist then as jsonObject is part of the keyvalue pair on the parameterlist JsonArray it will refetch the jsonObject which then again fetch the parameterlist and this process continues on and hence causing the StackOverflow Issue.
The Quick Solution is to create new JsonObject while assigning the parameterList,
JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);
JSONObject newJson = new JSONObject();
newJson.put("parameters", parameterlist);
System.out.println(newJson.toJSONString());
Lately i have been trying to consume a Rest service which returns the below rough sample JSON. It has objects which contains Arrays and arrays contains Strings.
{
Main Object:{
Object1:{
}
Object2:{
}
Object3:{
Array1[String1,String2,String3]
Array2[String1,String2,String3]
Array3[String1,String2,String3]
Array4[String1,String2,String3]
}
}}
My requirement is to get all the arrays and check which of the specific array contains specific/required String values and than get those strings to show in jsp.
I am using Spring MVC(Rest Template) but any java based solution would do the work.
P.S: I am New to WebServices.
use JSONArray to get it from below
JSONArray jsonArray = new JSONArray();
jsonArray = JSONObject.getJSONObject("Main Object").getJSONObject("Object3").getJSONArray("Array1");
//Iterate through the above array to get required String.
for the next array :
jsonArray = JSONObject.getJSONObject("Main Object").getJSONObject("Object3").getJSONArray("Array2");
//Iterate through the second array to get required String.
My question is how JSON works with serializing Sets because in my example , "propertyItems.getAddress().getCity().getProvinces().getCities()" is set of cities (Set) , i am iterating on it through for-each loop and putting it into json array. However it contains two cities i.e. MONTREAL and QuebecCity. But my url is only showing me "MONTREAL". Why its so? Json can't serialize sets provided to it?
JSONArray jarray = new JSONArray();
try {
JSONObject obj = new JSONObject();
for(Cities city:propertyItems.getAddress().getCity().getProvinces().getCities()){
obj.put("City Name",city.getCityname());
}
jarray.put(obj);
catch (JSONException e) {
logger.error(Constants.METHOD_INSIDE_MESSAGE +"getAuthors",e);
}
return jarray.toString();
}
** URL Output:** City Name :Montreal
The JSON specification states
The names within an object SHOULD be unique.
In the JSON parser/generator implementation you are using, ie. JSONObject, it seems the keys are unique. Every time you put an object with the same key, it overwrites the one added before it. Regardless of how many you put, the JSONObject will only contain one entry.
I have two json objects -- not strings, please -- that I want to combine into one json object as shown below.
The two objects:
JSONObject pen = {"plastic":"blue"}
JSONObject book = {"Maya":"Bird"}
Desired result:
JSONObject bag = {"plastic":"blue","Maya":"Bird"}
Is there an elegant way to do this? I mean without extracting the values of each pen and book and then re-insert them into bag using something like
bag.put("plastic","blue");
I am using org.codehaus.jettison.json.JSONObject if that information is necessary.
Naively, it seems like you could just do something like:
Iterator it = pen.keys();
while(it.hasNext())
{
String k = it.next();
bag.put(k, pen.getJSONObject(k));
}
// bag now has the combined key,value pairs.
But it has its obvious limitations.