Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON and Java: I store a long. I want to get the long. But: "org.json.JSONException: JSONObject is not a long". What's wrong?

When I try to make the following (using org.json):

JSONObject jo = new JSONObject();
jo.append("theLong", 1l);
Long theLong = jo.getLong("theLong");

I get the exception:

org.json.JSONException: JSONObject["theLong"] is not a long.
[...]
Caused by: java.lang.NumberFormatException: For input string: "[1]"

What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Remark:
Also unnecessarily boxing the long in a long jo.append("theLong", new Long(1l)); (to be sure that a Long is passed, not a long) wont help.

>Solution :

you should use jo.put("theLong", 1l);

append is used to "Append values to the array under a key. If the key does not exist in the JSONObject, then the key is put in the JSONObject with its value being a JSONArray containing the value parameter. If the key was already associated with a JSONArray, then the value parameter is appended to it."

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading