I'm trying to fill my JSONObject like this:
JSONObject json = new JSONObject(); json.put("Command", "CreateNewUser"); json.put("User", user);
user
is instance of basic class that contains fields like "FirstName", "LastName" etc.
Looks like I'm doing it wrong, because I get JSON like this:
{ "Command":"CreateNewUser", "User":"my.package.name.classes.User@2686a150" }
instead of "tree".
What is wrong with my code?
String message; JSONObject json = new JSONObject(); json. put("test1", "value1"); JSONObject jsonObj = new JSONObject(); jsonObj. put("id", 0); jsonObj.
UseJSON. stringify(jsonObject) to convert JSON Object to JSON String. Write the stringified object to file using fs. writeFile() function of Node FS module.
Since you use JSONObject
to represent non-primitive types, any instance passed to JSONObject.put(Object, Object)
will generate nested items (or trees).
JSONObject main = new JSONObject(); main.put("Command", "CreateNewUser"); JSONObject user = new JSONObject(); user.put("FirstName", "John"); user.put("LastName", "Reese"); main.put("User", user);
{ "User": { "FirstName": "John", "LastName": "Reese" }, "Command": "CreateNewUser" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With