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?


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.

Upvotes: 0

Views: 583

Answers (1)

xiaobo9
xiaobo9

Reputation: 121

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

Upvotes: 1

Related Questions