0

Basically I want to convert a JSONObject json into an integer (127181078). When I use this code:

int intOfReceivedID = Integer.parseInt(json);

I get this error message:

java.lang.NumberFormatException: Invalid int: 127181078"

When I use this code:

char[] charArray = json.toCharArray();
String stringCharArray = charArray.toString();
testTextView.setText(stringCharArray);

testTextView gives me: [C@2378e625

However, when I use this code:

preprocessedjson = String.valueOf(json);
testTextView.setText(preprocessedjson);

The TextView gives 127181078, but I get the some error when I parse the text to an integer.

  1. Can anybody tell me what going on here?
  2. And could you help me convert my JSONObject into an integer?

This is the php code:

$myfile = fopen($filename, 'r') or die("Unable to open file!");
echo fread($myfile,filesize($filename));
fclose($myfile);

This is the the makeHttpRequest:

JSONObject json = jparser.makeHttpRequest("http://myurl.nl/readspecifictextfile.php","POST",data);
5
  • Maybe you should explain why you want to convert an Object into a completely different incompatible type? Are you sure you don't want to retrieve an int value from the object at a particular key? Commented Sep 1, 2017 at 19:09
  • Try to parse it as a long instead of int Commented Sep 1, 2017 at 19:13
  • @Deadron: your comment gives me the idea I'm doing something stupid. I do indeed want to retrieve the int value of the object, but I don't understand the "at a particular key" of your comment. Thanks for helping! Commented Sep 1, 2017 at 19:16
  • @Navneet Krishna: I tried that as well, but then I get the same error. I choose the lenght of the integer to always fall within the bit limits. Commented Sep 1, 2017 at 19:17
  • You should post the code that is parsing the original JSON strring. If you are using a JSON parser(which you should be if you aren't) its generally trivial to retrieve a value from the object. It sounds like you might be parsing out the value by hand and messing up the value retrieval. Commented Sep 1, 2017 at 19:25

2 Answers 2

2

This question is confusing but from your error message it looks like this is a Java question that has nothing to do with JSON since your json String in this case looks like it does not in fact contain json(from the exception message).

It looks like the issue is your json value is a number plus additional spaces which Integer.parseInt does not handle.

Try

Integer.parseInt(json.trim())
Sign up to request clarification or add additional context in comments.

Comments

0
int intOfReceivedID = Integer.parseInt(json); 

I get this error message:

java.lang.NumberFormatException: Invalid int: 127181078"

This happens because there is a new-line character at the end of the ID which can not be parsed into an Integer.

When I use this code:

char[] charArray = json.toCharArray();
String stringCharArray = charArray.toString();
testTextView.setText(stringCharArray);

testTextView gives me: [C@2378e625

By default, calling toString() on an object prints the memory location of the object (in this case [C@2378e625). That's what's being displayed in the TextView.

However, when I use this code:

preprocessedjson = String.valueOf(json);
testTextView.setText(preprocessedjson);

The TextView gives 127181078, but I get the some error when I parse the text to an integer.

You'll get an error when parsing the text to an integer because it still has an invalid new-line character at the end.

If you are receiving a JSONObject from the server which only contains a long, then you can use the getLong() or optLong() methods to retrieve the integer value. The JSON parser automatically handles all the parsing and you don't need to do any additional work.

JSONObject json = jparser.makeHttpRequest("http://myurl.nl/readspecifictextfile.php","POST",data);
final long receivedId = json.optLong();

1 Comment

Thank you @W.K.S. for the explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.