-1

I am making an app that gets weather info from the web.

I am getting the whole JSONObject at once, instead of piece by piece. As a result of that, I can't call .getDouble for temperature, I call getString.

This returns a String, which I can't use the Math.round method on. I tried the .split(".") and it returns an array out of bounds exception, when I try to instert the sting into a text view.

If I call .toString() it gives me "[Ljava.lang.String;@7353879" in the textview.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
lakimens
  • 35
  • 10
  • Why exactly can you not call `getDouble` on a `JSONObject`? – OneCricketeer Oct 03 '16 at 19:01
  • 1
    that's not a string, that's a string[]. post your json, and show the code you tried to use to split it and to parse it. – njzk2 Oct 03 '16 at 19:02
  • I can't call getDouble, because it doesn't let me, it says resource not found. – lakimens Oct 03 '16 at 19:07
  • temp = mHourTest.getHour1().getString("temperature"); nodecimals = temp.split("."); mHourlyTemp.setText(nodecimals[0]); – lakimens Oct 03 '16 at 19:07
  • That's my .split() code, and it crashes with an arrayoutofbounds exception, when I add .toString() it returns [Ljava.lang.String;@7353879 – lakimens Oct 03 '16 at 19:07

2 Answers2

1

I did not understand exactly what you mean , If you intend to convert json data to double or float you can use :

Double.parseDouble(jsonObj.getString("x"));

or

Float.praseFloat(jsonObj.getString("x"));

like this :

float x = Float.praseFloat(jsonObj.getString("x"));

Update My Answer

for getting ride of decimal .... you can do this :

int x = ( (int) Double.parseDouble(objJson.getString("x")) );

String result = x + "" ;

Community
  • 1
  • 1
Nickan
  • 466
  • 5
  • 17
0

in order to get the text from the text field, you need to use the .getText() method, and then use .toString()

Carter Brainerd
  • 199
  • 2
  • 10