Why org.json.JSONObject getLong and getString not equal
android, json, org.json
Solution
As per the documentation here,
public long getLong (String name)
Returns the value mapped by name if it exists and is a long or can be coerced to a long. Note that JSON represents numbers as doubles, so this is lossy; use strings to transfer numbers via JSON.
EDIT: To see it yourself you can do this
JSONObject json = new JSONObject();
try {
json.put("test", "3537275240990320342");
String str = json.getString("test");
double dou = Double.parseDouble(str);
long lo = (long) dou;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You will see that the value of `lo` is exactly what you are getting when you do `getLong`
Problem
Code like this: ``` JSONObject contentObject = new JSONObject(content); JSONObject params = contentObject.getJSONObject("response_params"); Log.d("onTest", "channel_id:" + params.getString("channel_id")); Log.d("onTest", "channel_id:" + params.getLong("channel_id")); ``` Log print: ``` 08-21 12:46:21.470: DEBUG/onTest(25228): onMessage: content : {"response_params":{"appid":"1071410","channel_id":"3537275240990320342","user_id":"714893193627619861"},"request_id":1293438498} 08-21 12:46:21.480: DEBUG/onTest(25228): channel_id:3537275240990320342 08-21 12:46:21.480: DEBUG/onTest(25228): channel_id:3537275240990320128 ``` Why getLong with value 3537275240990320128,but get String with value 3537275240990320342?