Reputation: 725
I have a log file and each sentence contains a JSON object as per given below: -
{"action": "follow", "target": "FabianoCaruana", "timestamp": 1487149397523, "user": "GMHikaru"}
{"action": "tweet", "id": 16606634614844517897, "message": "woop #ruby", "timestamp": 1487149445603, "user": "hugopeixoto"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446020, "user": "spambot"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446592, "user": "noob_saibot"}
{"action": "tweet", "id": 14936153188153171323, "message": "woop #vim", "timestamp": 1487149463067, "user": "eevee"}
{"action": "follow", "target": "eevee", "timestamp": 1487149466209, "user": "pkoch"}
{"action": "tweet", "id": 1801829421162967878, "message": "woop #processes", "timestamp": 1487149468671, "user": "r00k"}
{"action": "retweet", "target_id": 1801829421162967878, "timestamp": 1487149469555, "user": "noob_saibot"}
{"action": "follow", "target": "r00k", "timestamp": 1487149472418, "user": "pkoch"}
I am reading each line using JSON parser and storing into a String for further processing
String line = {"action": "tweet",
"id": 16606634614844517897,
"message": "woop #ruby",
"timestamp": 1487149445603,
"user": "hugopeixoto"};
while parsing above String using "json-simple-1.1.1.jar", I am getting an error : -
java.lang.NumberFormatException: For input string: "16606634614844517897"
Code to parse above JSON string: -
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(line);
String name =(String) json.get("user");
I want to print the "user" name present in above JSON string.
Obviously, the id is a Long format, hence the exception is.
Could you please suggest me a workaround to eliminate above exception. I tried all combinations but didn't work.
Upvotes: 0
Views: 2147
Reputation: 21995
Obviously, the id is a Long format
Nope, sorry the maximum Long
value is the following :
9223372036854775807 <-- has a length of 19
while yours
16606634614844517897 <- has a length of 20
I see two simple solutions to your problem.
If the id only serves the purpose of unique identification (which is probably the case here), you should be using a String
instead?
If you'll need to use the id and compare its numeric value or perform numeric operations on it, which I doubt, you could still use a BigInteger
like the following
new BigInteger("16606634614844517897");
Upvotes: 2