-2

Actually "Strictly speaking, json is untyped, so you can't send it as an integer, it has to be a string." is said in that question 2 YEARS AGO. So i wonder if there are any changes in JSON to get integer from a JSON Object. I try to call integer by this way;

static int versionCode=0;
jsonObject = new JSONObject(result);
JSONObject queryJSONObject = jsonObject.getJSONObject("query");
versionCode =queryJSONObject.getInt("versionCode"); 

And the JSON is like this;

{"query":{"versionCode":1,....}}

Can anything be done?

Thanks.

Community
  • 1
  • 1
ufukcakir
  • 61
  • 2
  • 11
  • Were you asking about sending or receiving/getting the integer? Your title is contradicting with your code. Also, maybe related: [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Andrew T. May 06 '14 at 09:00
  • You are right, it should be receiving. Thanks for comment. – ufukcakir May 06 '14 at 09:14

2 Answers2

1

follow this Tutorial you can use getString() function to get value and latter you can convert it like this int foo = Integer.parseInt("1234"); in integer according to your need

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Irshad Khan
  • 794
  • 6
  • 21
-1

JSON has a number element, see JSON.org enter image description here

JSON is a sequence of characters. But I disagree that a number is a string in this context. IMHO for me a string is wrapped in double quotes (which a number isn't).

Your code should work: json.getInt("versionCode") Source from Android Developer

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • There is no problem in string calls, when l add that part app stops, when I comment it out, app works. Thanks for answer. – ufukcakir May 06 '14 at 09:36
  • Can you add a Try/Catch clausule and see what Exception is thrown (or look in LogCat) – RvdK May 06 '14 at 13:15