【问题标题】:Error occurred While try to parsing JSON String using java尝试使用 java 解析 JSON 字符串时发生错误
【发布时间】:2015-07-22 20:48:10
【问题描述】:

我正在尝试使用 java 解析 JSON 字符串。我不知道该怎么做,我在互联网上搜索了很多,我得到了一些想法。有了它,我有构建代码,但它不起作用。当尝试执行我的代码时,它会引发错误。我无法解决错误。

下面是我的代码:

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

public class JSOnNStringParsing {

    public static void main(String[] args) {        
        try
        {
            BufferedReader read=new BufferedReader(new FileReader("D:\\Kavi works\\OutputFiles\\JSON_String.txt"));
            String line=read.readLine();
            while(line!=null)
            {
                System.out.println("Line = "+line);
                JSONParser jsonParser = new JSONParser();
                JSONObject jsonObject = (JSONObject) jsonParser.parse(line);
                System.out.println("Read : "+jsonObject.get("read"));
                JSONArray network = (JSONArray) jsonObject.get("network");
                Iterator<String> iterator = network.iterator();
                while (iterator.hasNext()) {
                    System.out.println(iterator.next());
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("Error occured :"+e);
        }
    }
}

我的 JSON 字符串在该文件中:

{"read":"2015-05-07T19:30:48.165009273+05:30","network":{"rx_bytes":11124,"rx_packets":116,"rx_errors":0,"rx_dropped":0,"tx_bytes":648,"tx_packets":8,"tx_errors":0,"tx_dropped":0},"cpu_stats":{"cpu_usage":{"total_usage":157158138204,"percpu_usage":[157158138204],"usage_in_kernelmode":49530000000,"usage_in_usermode":58420000000},"system_cpu_usage":258964110000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":73969664,"max_usage":74600448,"stats":{"active_anon":73928704,"active_file":4096,"cache":86016,"hierarchical_memory_limit":18446744073709551615,"inactive_anon":4096,"inactive_file":32768,"mapped_file":32768,"pgfault":62880,"pgmajfault":0,"pgpgin":34656,"pgpgout":34482,"rss":73883648,"rss_huge":67108864,"total_active_anon":73928704,"total_active_file":4096,"total_cache":86016,"total_inactive_anon":4096,"total_inactive_file":32768,"total_mapped_file":32768,"total_pgfault":62880,"total_pgmajfault":0,"total_pgpgin":34656,"total_pgpgout":34482,"total_rss":73883648,"total_rss_huge":67108864,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"failcnt":0,"limit":2099310592},"blkio_stats":{"io_service_bytes_recursive":[],"io_serviced_recursive":[],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]}}

当我执行我的代码时,它会抛出如下错误:

Line = {"read":"2015-05-07T19:30:48.165009273+05:30","network":{"rx_bytes":11124,"rx_packets":116,"rx_errors":0,"rx_dropped":0,"tx_bytes":648,"tx_packets":8,"tx_errors":0,"tx_dropped":0},"cpu_stats":{"cpu_usage":{"total_usage":157158138204,"percpu_usage":[157158138204],"usage_in_kernelmode":49530000000,"usage_in_usermode":58420000000},"system_cpu_usage":258964110000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":73969664,"max_usage":74600448,"stats":{"active_anon":73928704,"active_file":4096,"cache":86016,"hierarchical_memory_limit":18446744073709551615,"inactive_anon":4096,"inactive_file":32768,"mapped_file":32768,"pgfault":62880,"pgmajfault":0,"pgpgin":34656,"pgpgout":34482,"rss":73883648,"rss_huge":67108864,"total_active_anon":73928704,"total_active_file":4096,"total_cache":86016,"total_inactive_anon":4096,"total_inactive_file":32768,"total_mapped_file":32768,"total_pgfault":62880,"total_pgmajfault":0,"total_pgpgin":34656,"total_pgpgout":34482,"total_rss":73883648,"total_rss_huge":67108864,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"failcnt":0,"limit":2099310592},"blkio_stats":{"io_service_bytes_recursive":[],"io_serviced_recursive":[],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]}}
Error occured :java.lang.NumberFormatException: For input string: "18446744073709551615"

请帮我解决这个问题,在此先感谢

【问题讨论】:

  • 请显示 full 堆栈跟踪 - 而不仅仅是它的第一行。
  • 18446744073709551615 = 2^64 但带符号的 long 仅为 2^63-1

标签: java json


【解决方案1】:

184467440737095516152^64,直到 Java 8,long 的最大值是 2^63 - 1(即 9223372036854775807)。在 Java 8 中,您可以有一个无符号长整数 2^64,但是您将自己绑定到特定的 Java 最低版本。

检查 org.json.simple.parser.JSONParser, etc 的来源,BigInteger 和 BigDecimal 都没有被提及,所以看起来你需要一个替代解析器。

一些 JSON 解析器可以让您明确控制这种行为,例如在杰克逊你有

mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
mapper.enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);

this question 也有一些有用的信息。

【讨论】:

  • 你如何“使用”BigInteger 来修复 OP 的代码?举个例子会很有帮助。
【解决方案2】:

您的 json 在 Java 中解析错误。 Long 的最大值在 Java 中是 9223372036854775807 但在你的 json 中你超过了它。 Json 可以访问非常长的数字,但 java 不会。

只需将 json 中的数字类型更改为 String 即可。看下面的例子。

{
  "read": "2015-05-07T19:30:48.165009273+05:30",
  "network": {
    "rx_bytes": 11124,
    "rx_packets": 116,
    "rx_errors": 0,
    "rx_dropped": 0,
    "tx_bytes": 648,
    "tx_packets": 8,
    "tx_errors": 0,
    "tx_dropped": 0
  },
  "cpu_stats": {
    "cpu_usage": {
      "total_usage": 157158138204,
      "percpu_usage": [
        157158138204
      ],
      "usage_in_kernelmode": 49530000000,
      "usage_in_usermode": 58420000000
    },
    "system_cpu_usage": 258964110000000,
    "throttling_data": {
      "periods": 0,
      "throttled_periods": 0,
      "throttled_time": 0
    }
  },
  "memory_stats": {
    "usage": 73969664,
    "max_usage": 74600448,
    "stats": {
      "active_anon": 73928704,
      "active_file": 4096,
      "cache": 86016,
      "hierarchical_memory_limit": "18446744073709552000898498494949849849849849849849849849841998498498484984",
      "inactive_anon": 4096,
      "inactive_file": 32768,
      "mapped_file": 32768,
      "pgfault": 62880,
      "pgmajfault": 0,
      "pgpgin": 34656,
      "pgpgout": 34482,
      "rss": 73883648,
      "rss_huge": 67108864,
      "total_active_anon": 73928704,
      "total_active_file": 4096,
      "total_cache": 86016,
      "total_inactive_anon": 4096,
      "total_inactive_file": 32768,
      "total_mapped_file": 32768,
      "total_pgfault": 62880,
      "total_pgmajfault": 0,
      "total_pgpgin": 34656,
      "total_pgpgout": 34482,
      "total_rss": 73883648,
      "total_rss_huge": 67108864,
      "total_unevictable": 0,
      "total_writeback": 0,
      "unevictable": 0,
      "writeback": 0
    },
    "failcnt": 0,
    "limit": 2099310592
  },
  "blkio_stats": {
    "io_service_bytes_recursive": [],
    "io_serviced_recursive": [],
    "io_queue_recursive": [],
    "io_service_time_recursive": [],
    "io_wait_time_recursive": [],
    "io_merged_recursive": [],
    "io_time_recursive": [],
    "sectors_recursive": []
  }
}

编辑:@Jon Skeet 发表评论后,我意识到,他对 Json 的看法是正确的。在不同的 json 解析器中,您可以轻松解析 json,它会将其处理为 BigInteger。 BigInteger 没有限制。

这是一个例子:

try{
            String line = "{'read':'2015-05-07T19:30:48.165009273+05:30','network':{'rx_bytes':11124,'rx_packets':116,'rx_errors':0,'rx_dropped':0,'tx_bytes':648,'tx_packets':8,'tx_errors':0,'tx_dropped':0},'cpu_stats':{'cpu_usage':{'total_usage':157158138204,'percpu_usage':[157158138204],'usage_in_kernelmode':49530000000,'usage_in_usermode':58420000000},'system_cpu_usage':258964110000000,'throttling_data':{'periods':0,'throttled_periods':0,'throttled_time':0}},'memory_stats':{'usage':73969664,'max_usage':74600448,'stats':{'active_anon':73928704,'active_file':4096,'cache':86016,'hierarchical_memory_limit':18446744073709552000,'inactive_anon':4096,'inactive_file':32768,'mapped_file':32768,'pgfault':62880,'pgmajfault':0,'pgpgin':34656,'pgpgout':34482,'rss':73883648,'rss_huge':67108864,'total_active_anon':73928704,'total_active_file':4096,'total_cache':86016,'total_inactive_anon':4096,'total_inactive_file':32768,'total_mapped_file':32768,'total_pgfault':62880,'total_pgmajfault':0,'total_pgpgin':34656,'total_pgpgout':34482,'total_rss':73883648,'total_rss_huge':67108864,'total_unevictable':0,'total_writeback':0,'unevictable':0,'writeback':0},'failcnt':0,'limit':2099310592},'blkio_stats':{'io_service_bytes_recursive':[],'io_serviced_recursive':[],'io_queue_recursive':[],'io_service_time_recursive':[],'io_wait_time_recursive':[],'io_merged_recursive':[],'io_time_recursive':[],'sectors_recursive':[]}}";
            line = line.replaceAll( "'", "\"" );
            while( line != null ){

                JsonObject asJsonObject = new JsonParser().parse( line ).getAsJsonObject().get( "network" ).getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                for( Entry<String, JsonElement> entry : entrySet ){
                    System.out.println( entry.getKey() + " : " + entry.getValue() );
                }
            }
        }
        catch( Exception e ){
            System.out.println( "Error occured :" + e );
        }

我用gson 来解析你的json。谢谢@Jon Skeet。

【讨论】:

  • 有多种方式可以由一个好的JSON解析器在Java中处理,例如使用BigIntegerBigDecimal或@ 987654328@。这不是固有在 Java 中解析 JSON 的问题 - 这只是 JSON-simple、IMO 的限制。
【解决方案3】:

对我来说,这似乎是 JSON 中的一个错误。使用org.json 解析器,它工作正常。

问题在于它试图将值解析为整数,并且它超出了普通 Java 原始整数类型的范围。它可以将其解析为BigInteger,或者解析为double(这将更符合JSON,而不是实际区分整数和浮点值)。

如果您在 JSON 中的值末尾添加 .0,它在 JSON-simple 中可以正常工作,但在我看来您不应该这样做。

【讨论】:

  • “在我看来你不应该这样做”——完全同意。由于此输入来自文件,因此我认为任何需要更改输入文件(或其他外部输入源)的解决方案都是非解决方案。
  • 关于此问题的任何解决方法,我都会遇到完全相同的错误。我无法将 .0 放在末尾,因为 JSON 数据来自我需要解析的日志文件。
  • @KushalShinde 我认为解决方法是使用更好的解析器...
  • @JonSkeet 谢谢。我切换到 gson,我的问题得到了解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2022-09-22
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多