尝试使用Java解析JSON字符串时发生错误

尝试使用Java解析JSON字符串时发生错误,第1张

尝试使用Java解析JSON字符串时发生错误

您的json在Java中解析错误。在Java中,最大值

Long
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。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4915784.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存