Page 1 of 1

JSONObject: int values as key doesn't work properly

Posted: 08 Apr 2011, 04:24
by BigFIsh
Just something to keep in mind:

Code: Select all

JSONObject test = new JSONObject();
int userId = 10;
test.put(userId, "hello");
trace("Result: " + test.containsKey(userId));
Result traces out false. Same problem will occur for test.get(userId). However, if you send the 'test' JSONObject back to client, the data is there which took me a while to narrow down the problem.

Code: Select all

JSONObject test = new JSONObject();
int userId = 10;
test.put(userId, "hello");
trace("Result: " + test.containsKey(String.valueOf(userId)));
Result now traces out true. Casting the userId as String is the workaround.