【问题标题】:java.lang.NumberFormatException: For input string: "18446744073709551615" [duplicate]java.lang.NumberFormatException:对于输入字符串:“18446744073709551615”[重复]
【发布时间】:2019-03-19 12:39:33
【问题描述】:

首先.. 如果缺少某些信息,请询问,我会提供所需的输入。

我的桌面上有一个 .json 文件,现在我必须使用 java 并且必须从 .json 文件中提取值。 这是我的任务:**编写一个提取值的脚本 “自动 -> 内核 -> 发布” 从附加的 json 文件中取出。

示例:输出:

3.16.0-5-amd64**

json文件有27k+行,不知道这个会不会有问题。

.json 文件包括:

   1 {
   2   “results”: 3,
   3   “rows”: [
   4     {
   5   “name”: “test”,
   6   “chef_environment”: “office”,
   7   “json_class”: “Chef::Node”,
   8   “automatic”: {
   9     “nginx”: {
  10       “version”: null,
  11       “configure_arguments”: [
  12
  13       ],
  14       “prefix”: null,
  15       “conf_path”: null
  16     },
.....
  98 “kernel”: {
  99 “name”: “Linux”,
 100 “release”: “3.16.0-5-amd64"

这是我现在的代码:

package json;

import java.io.*;
import java.io.File;
import java.io.FileReader;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import java.util.Iterator;

public class json {

    public static void main(String[] args) throws Exception {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(new FileReader("C:\\Users\\admin-elias\\Desktop\\nginx.json"));
            JSONObject jsonObject = (JSONObject) obj;

            String automatic = (String) jsonObject.get("automatic");
            String kernel = (String) jsonObject.get("kernel");
            JSONArray release = (JSONArray) jsonObject.get("release");

            System.out.println("automatic: "+automatic);
            System.out.println("kernel: "+kernel);
            System.out.println("release: "+release);
            ;



            } catch (Exception e) {
                e.printStackTrace();
            }
    } 
}

当我尝试运行程序时,我收到以下错误:

java.lang.NumberFormatException: For input string: "18446744073709551615"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Long.parseLong(Long.java:692)
    at java.base/java.lang.Long.valueOf(Long.java:1144)
    at org.json.simple.parser.Yylex.yylex(Yylex.java:660)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
    at json.json.main(json.java:18)

我做错了什么?我对 java 比较陌生,我需要让这个程序运行,所以每一个建议或帮助都会非常好。 :)

编辑:

我终于得到了完美运行的代码:

public class json {

    public static void main(String[] args) throws Exception {
        try {
            JsonReader jsonReader = new JsonReader(new FileReader("filename"));
            Gson gson = new Gson();

            JsonElement json = gson.fromJson(jsonReader, JsonElement.class);

            //JsonElement json = new JsonParser().parse(jsonString);

            JsonArray array = json.getAsJsonObject().get("rows").getAsJsonArray();
            int length = array.size();
            for (int i = 0; i < length; i++) {
                System.out.println(array.get(i).getAsJsonObject().get("automatic").getAsJsonObject().get("kernel").getAsJsonObject().get("release").getAsString());
            }

        } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

【问题讨论】:

  • 这个数字 18446744073709551615 不适合长
  • Gson 库支持开箱即用的大整数。您是否可以使用 Gson 而不是简单的解析器? mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5
  • 好吧@PatrickDorn 那我想我必须写一个完整的新代码?尚未使用 Gson,但也许值得一试。
  • 不应该太难。 Gson 支持 FileReader 作为输入,并且有自己的 com.google.gson.JsonObject 和 com.google.gson.JsonArray 类。解析:JsonObject obj = gson.fromJson(fileReader, JsonObject.class);
  • 好的酷@PatrickDorn 谢谢你的帮助,我会试试看:)

标签: java eclipse


【解决方案1】:

价值

18446744073709551615

超过maximum value of a long:

9223372036854775807

您应该将其解析为 BigInteger

【讨论】:

  • 好的,很高兴知道这一点。如何解析成 BigInteger?我必须在我的代码中进行哪些更改?
  • 嗯......根据"exceeding LONG.MAX_VALUE in JSON Simple parser",这似乎不可能使用JSON Simple。所以你必须寻找另一个解析器。
  • 好的..好吧..我会试试的,谢谢:)
【解决方案2】:

它试图将值解析为整数/长整数并且它超出范围

18446744073709551616 = 2^64 - 1

整数.MAX_VALUE = 2^31 - 1

Long.MAX_VALUE = 2^63 - 1

【讨论】:

  • 好的,有道理。但是我可以在我的代码中做些什么来避免这个问题呢?
  • 你在使用 org.json.simple 吗?
  • 是的,正如您在顶部的导入中已经看到的那样:)
  • 尝试使用 org.json.JSONObject 而不是 org.json.simple.JSONObject 。这是 2 个不同的库
  • 好的,做到了,但错误仍然存​​在:/
猜你喜欢
  • 2017-09-02
  • 2013-09-16
  • 2020-03-11
  • 2015-04-19
  • 2018-06-08
  • 1970-01-01
  • 2012-12-05
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多