2

I am trying to read and parse a json file using simple.json in Java. However, on floating point numbers I get error. How should I parse floating point numbers?

The JSON File is like:

[
  {
    "region":"NF",
    "destination":"d1",
    "source":"s1",
    "time":2003,
    "value":0.1
  },
  {
    "region":"NF",
    "destination":"d2",
    "source":"s2",
    "time":2004,
    "value":0.002
  },
]

My code to parse it is:

JSONArray jsonArray = (JSONArray)obj;
Iterator<JSONObject> iterator = jsonArray.iterator();

while(iterator.hasNext()){
    JSONObject jsonObject = iterator.next();
    String region = (String) jsonObject.get("region");
    String src = (String) jsonObject.get("source");
    String dst = (String) jsonObject.get("destination");
    long time = (long) jsonObject.get("time");
    long val = (long) jsonObject.get("value");
}
Joel
  • 4,732
  • 9
  • 39
  • 54
hAlE
  • 1,283
  • 3
  • 13
  • 19
  • 1
    what is the error you are getting? A sample JSON input, and code reading this input would help understand the problem better! – Vikdor Feb 10 '14 at 23:57
  • Can you show us something that would enable someone to answer this? Like the JSON file that you're trying to parse? Or the code that you're using? Maybe even the error message? Anything, so we can see what the problem is. Thank you. – Dawood ibn Kareem Feb 10 '14 at 23:57
  • Don't know anything about "simple.json", but a floating-point number should generally come through from JSON as a subclass of Number. You should be able to apply `doubleValue()` to it without having to determine the specific class returned. – Hot Licks Feb 11 '14 at 00:03
  • I completed my question. Any ideas? – hAlE Feb 11 '14 at 00:07
  • So your error is that `0.002` gets rounded down to `0` when you cast it to `long`, is that right? – Dawood ibn Kareem Feb 11 '14 at 00:09
  • No, it just produces error. java.lang.Double cannot be cast to java.lang.Long – hAlE Feb 11 '14 at 00:10
  • Even when I cast it to doube. – hAlE Feb 11 '14 at 00:10
  • JSON-Lists mustn't contain any trailing commas. However some parsers are patient with this rule, don't know about simple.json's behaviour. – Raoul Feb 11 '14 at 00:12
  • Can you link to the Java library (ideally the Javadocs) that you are using? – maerics Feb 11 '14 at 00:15
  • 1
    You're receiving a `Double`, not a `double`. You must use one of the methods of `Number` to retrieve the value. – Hot Licks Feb 11 '14 at 00:27
  • `Number number = (Number) jsonObject.get("value"); double val = number.doubleValue();` (or `long val = number.longValue();`) – Hot Licks Feb 11 '14 at 00:29

3 Answers3

11

If you want to store a floating point number, then you need a variable of that type, i.e., a double.

double val = ((Number)jsonObject.get("value")).doubleValue();

In this case, the get() method should return an instance of java.lang.Number. Then you can call the doubleValue() method to store the floating point value.

Joel
  • 4,732
  • 9
  • 39
  • 54
  • Note that OP is (presumably) asking about the [json-simple library](https://code.google.com/p/json-simple/), not the [JSON.org library](http://www.json.org/java/index.html). – maerics Feb 11 '14 at 00:16
  • @maerics Edited answer to fit the library requirement, thanks! – Joel Feb 11 '14 at 00:20
  • Does `get` return a `Number`? If it returns `Object` then `doubleValue` will not apply. – Hot Licks Feb 11 '14 at 00:36
  • In which case, this won't actually compile. – Dawood ibn Kareem Feb 11 '14 at 01:11
  • Thanks! Issue solved. In simple.json values are returned as Object. So a casting to Number is required. Number val = (Number)jsonObject.get("value"); double value = val.doubleValue(); – hAlE Feb 11 '14 at 01:15
  • Actually, I am not sure what it returns. So I'll use a cast to Number in case. – Joel Feb 11 '14 at 01:52
1

In Java EE 7, use jsonObject.getJsonNumber("key").doubleValue() to get the double value.

See: https://docs.oracle.com/javaee/7/api/javax/json/JsonNumber.html

CatSleeping
  • 81
  • 1
  • 3
0

I would guess that this library (I'm guessing it's json-simple from your tag) returns numeric types as type double.

Double value = (Double) jsonObject.get("value");

For example (tested and working with json-simple-1.1.1):

String jsonString = "{\"foo\":1.23}";
JSONObject obj = (JSONObject) JSONValue.parse(jsonString);
Double d = (Double) obj.get("foo"); // => 1.23
maerics
  • 151,642
  • 46
  • 269
  • 291
  • I get this error when I do this: Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double – hAlE Feb 11 '14 at 00:09
  • @user1720860: note that both the type and the cast must be "Double". – maerics Feb 11 '14 at 00:09
  • 1
    The class used should be Number, which is "agnostic" and will cover either Double or Long. Generally the parser will return integer values as Long and fractional values as Double. – Hot Licks Feb 11 '14 at 00:34