问题
I am currently using the JSON Simple library for parsing JSON in my application. I have run into an issue while parsing that is throwing the following error:
java.lang.NumberFormatException: For input string: "107701583138930170002"
I have narrowed this down to what I believe is the issue, the number is to big to be a LONG type. The problem is that I do not have control over this number otherwise I would make it a string. Is there a way to make JSON Simple parse this into a BigInt instead of LONG?
回答1:
The json-simple package downloadable from Google Code doesn't seem to have been updated since 2009. If you look on github there was a commit in Nov 2011 that adds support for numbers outside the Long
range.
You will probably have to get the source from github and build a more current version of the library. Looks like json-simple is not being maintained.
回答2:
If you are using Json Simple's streaming parser, you can actually use your json simple ContentHandler with jackson as well with relatively little trouble. I did this for my jsonj project: https://github.com/jillesvangurp/jsonj/blob/master/src/main/java/com/github/jsonj/tools/JsonParserNg.java which is an alternative to the JsonParser.java in the same package that uses jackson instead of jsonsimple to drive the json simple handler.
The jackson parser supports BigInteger. The key method in the class I linked above simply calls the appropriate jsonsimple handler method in a switch statement. Where I call parser.getLongValue(), you can instead call parser.getBigIntegerValue()
来源:https://stackoverflow.com/questions/24064028/exceeding-long-max-value-in-json-simple-parser