Home > Back-end >  JSON and Java: I store a long. I want to get the long. But: "org.json.JSONException: JSONObject
JSON and Java: I store a long. I want to get the long. But: "org.json.JSONException: JSONObject

Time:04-10

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?


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.

CodePudding user response:

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."

  • Related