尝试使用java解析JSON String时发生错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了尝试使用java解析JSON String时发生错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7754字,纯文字阅读大概需要12分钟。
内容图文
我正在尝试使用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"
请帮我解决这个问题,提前谢谢
解决方法:
你的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.
内容总结
以上是互联网集市为您收集整理的尝试使用java解析JSON String时发生错误全部内容,希望文章能够帮你解决尝试使用java解析JSON String时发生错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。