Sorry, I started teach java few days ago and didn't find a solution in google. I know that I can do this:
JSONOblect x = new JSONObject;
JSONOblect y = new JSONObject;
y.put("a","b");
x.put("c",y);
I just want to ask is there any way to add one object into other something like
JSONObject x = new JSONObject();
x.put("c", new JSONObject{"a":"b"});
I have a lot of objects one inside other and if I declare each as variable - it will be very long.
Assuming you're using org.json.JSONObject
, you can use the fact that JSONObject.put
returns the value it's called on. For example:
JSONObject x = new JSONObject().put("c", new JSONObject().put("a", "b"));
You might want to consider embedding the data as JSON into your application though, and just parsing it. Aside from anything else, that's considerably easier to read and modify...
See more on this question at Stackoverflow