【发布时间】:2018-02-10 21:26:21
【问题描述】:
基本上我想将 JSONObject json 转换为整数 (127181078)。当我使用此代码时:
int intOfReceivedID = Integer.parseInt(json);
我收到此错误消息:
java.lang.NumberFormatException: Invalid int: 127181078"
当我使用这段代码时:
char[] charArray = json.toCharArray();
String stringCharArray = charArray.toString();
testTextView.setText(stringCharArray);
testTextView 给我:[C@2378e625
但是,当我使用此代码时:
preprocessedjson = String.valueOf(json);
testTextView.setText(preprocessedjson);
TextView 给出 127181078,但是当我将文本解析为整数时出现一些错误。
- 谁能告诉我这里发生了什么?
- 您能帮我将 JSONObject 转换为整数吗?
这是php代码:
$myfile = fopen($filename, 'r') or die("Unable to open file!");
echo fread($myfile,filesize($filename));
fclose($myfile);
这是makeHttpRequest:
JSONObject json = jparser.makeHttpRequest("http://myurl.nl/readspecifictextfile.php","POST",data);
【问题讨论】:
-
也许您应该解释一下为什么要将 Object 转换为完全不同的不兼容类型?您确定不想从对象的特定键处检索 int 值吗?
-
尝试将其解析为 long 而不是 int
-
@Deadron:你的评论让我觉得我在做一些愚蠢的事情。我确实想检索对象的 int 值,但我不明白您评论的“特定键”。感谢您的帮助!
-
@Navneet Krishna:我也试过了,但是我得到了同样的错误。我选择整数的长度始终在位限制内。
-
您应该发布解析原始 JSON 字符串的代码。如果您使用的是 JSON 解析器(如果不是,则应该使用),从对象中检索值通常很简单。听起来您可能正在手动解析值并弄乱值检索。