- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个日志文件,每个句子都包含一个 JSON 对象,如下所示:-
{"action": "follow", "target": "FabianoCaruana", "timestamp": 1487149397523, "user": "GMHikaru"}
{"action": "tweet", "id": 16606634614844517897, "message": "woop #ruby", "timestamp": 1487149445603, "user": "hugopeixoto"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446020, "user": "spambot"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446592, "user": "noob_saibot"}
{"action": "tweet", "id": 14936153188153171323, "message": "woop #vim", "timestamp": 1487149463067, "user": "eevee"}
{"action": "follow", "target": "eevee", "timestamp": 1487149466209, "user": "pkoch"}
{"action": "tweet", "id": 1801829421162967878, "message": "woop #processes", "timestamp": 1487149468671, "user": "r00k"}
{"action": "retweet", "target_id": 1801829421162967878, "timestamp": 1487149469555, "user": "noob_saibot"}
{"action": "follow", "target": "r00k", "timestamp": 1487149472418, "user": "pkoch"}
我正在使用 JSON 解析器读取每一行并将其存储到字符串中以供进一步处理
String line = {"action": "tweet",
"id": 16606634614844517897,
"message": "woop #ruby",
"timestamp": 1487149445603,
"user": "hugopeixoto"};
使用“json-simple-1.1.1.jar”解析上面的字符串时,出现错误:-
java.lang.NumberFormatException: For input string: "16606634614844517897"
解析上述 JSON 字符串的代码:-
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(line);
String name =(String) json.get("user");
我想打印上面 JSON 字符串中存在的“用户”名称。
显然,id 是 Long 格式,因此异常(exception)。
您能否建议我一个解决方法来消除上述异常。我尝试了所有组合,但没有成功。
最佳答案
Obviously, the id is a Long format
不,抱歉,最大 Long
值如下:
9223372036854775807 <-- has a length of 19
当你的时候
16606634614844517897 <- has a length of 20
<小时/>
我看到两个简单的解决方案可以解决您的问题。
如果 id 仅用于唯一标识的目的(这里可能就是这种情况),您应该使用 String
来代替?
如果您需要使用 id 并比较其数值或对其执行数值运算(我对此表示怀疑),您仍然可以使用如下所示的 BigInteger
new BigInteger("16606634614844517897");
关于java - 解析包含 Long 值的 JSON 字符串时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543625/
我正在尝试使用 Integer.parseInt(simNumber) 将 String 转换为 int,但我收到错误消息。如何将 String 转换为 int?我在网上搜索了一下,找到了Intege
每次我尝试启动我的代码时,我总是遇到同样的错误: java.lang.NumberFormatException: For input string: "x" at java.lang.Numbe
在检查我的错误处理代码时偶然发现了这个。当您调用 Integer.parseInt(null) 时,Java 抛出 NumberFormatException 而 Float.parseFloat(n
如果有人可以看看我在 Selenium 方面遇到的问题。我尝试使用以下方法访问上述计算器中的结果: public String getAttributeValue(WebElement element
嗯,我很无聊,所以我想做一个二进制>十进制转换器。我将转换器的上限设置为 30 位数字 (1073741823) 或 (0111111111111111111111111111111)。我遇到的问题是
在以下代码中,Clojure (1.2) 打印出错误消息: (try (let [value "1,a"] (map #(Integer/parseInt %) (.split value
String str="123456.7855456677"; ParsePosition parsePosition = new ParsePosition(0); NumberFormat num
代码: String myVar = "1255763710960"; int myTempVar=0; try { myTempVar = Integer.valueOf(myVar); }
我在NumberFormatException代码中收到Grails错误,该错误是将电影分类到数据库中。该错误表明它来自cron插件。我已经完成了研究,并且一直在尝试使用NumberFormatExc
我在将字符串解析为原语时遇到问题。 File file1 = new File("bankData.txt"); Scanner sc = new Scanner(fi
所以我试图从日期列表中查找最新日期,但我不断收到 NumberFormatException。有什么办法可以解决这个问题吗? import java.util.*; public class Date
如果使用变量 bin1 它不会转换,但是如果我用 bin2 替换参数,它似乎可以工作。 我尝试使用 long 而不是 int。还是一样。 public class Test{ public s
我正在尝试找出如何在代码中捕获数字格式异常错误,这样如果用户在字符串中输入字母并且我的程序尝试将其解析为 int 我的程序将不会抛出错误而是停止并返回一个 boolean 值。我还试图了解,如果 tr
该程序的本质目的是将学生信息写入文件并从同一文件中读取。该程序包括一个 if 语句,用于判断学生是否处于良好信誉或留校察看状态,每个语句都有各自的文件(goodstand.txt 和 probatio
我正在用 Python/WLST 编写代码,以自动连接、启动和停止 weblogic 中的托管服务器。当我启动 Python 时,出现以下错误。 异常的含义是什么?我该如何解决它? Initializ
这是我的代码: public void setAreaAccessPoints(){ String mac = "",essid = "",status = ""; int stre
我正在用 Java 开发一个应用程序,发现了这个奇怪的行为: 如果通过控制面板将区域设置格式设置为匈牙利语(系统默认值),我会收到此异常,但如果我将其设置为英语格式,则它可以正常工作。也适用于虚拟 M
我正在尝试制作一个计算器应用程序,但不幸的是我无法解析字符串。 应用程序接受输入并将其显示在 EditText et2 中。当按下“等于”键时,计算器将执行以下工作。 et2 = (EditText)
我有一个文件,其中包含许多电影标题及其年份、5 星评级和长度。我正在尝试读取该文件并将标题、年份、评级和长度存储为变量。我遇到的问题是在我的检索年份的代码中。该代码可以编译,但在运行时,当它到达具有多
我正在解决亚马逊 Interview Street 网站上的 Connected Sets 问题 https://amazon.interviewstreet.com/challenges我的代码对于
我是一名优秀的程序员,十分优秀!