Error occurred While try to parsing JSON String using java -


i'm trying parse json string using java. don't know how that, searched lot in internet , got idea. have build code, doesn't work. when try execute code throw error. couldn't solve error.

see below code:

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);         }     } } 

my json string in file:

{"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":[]}} 

when execute code throws error below:

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: input string: "18446744073709551615" 

please me solve issue, in advance

your json wrong parse in java. maximum value of long 9223372036854775807 in java in json exceeding it. json can access really long numbers, java won't.

just change number types in json string after go. @ following example.

{   "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": []   } } 

edit: after @jon skeet's comment realised, he's right json simple. in different json parser can easilly parse json, , handle biginteger. biginteger has no limit.

here's example:

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 );         } 

i used gson parse json. @jon skeet.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -