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");
}
11
  • 1
    what is the error you are getting? A sample JSON input, and code reading this input would help understand the problem better! Commented Feb 10, 2014 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. Commented Feb 10, 2014 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. Commented Feb 11, 2014 at 0:03
  • I completed my question. Any ideas? Commented Feb 11, 2014 at 0:07
  • 1
    You're receiving a Double, not a double. You must use one of the methods of Number to retrieve the value. Commented Feb 11, 2014 at 0:27

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that OP is (presumably) asking about the json-simple library, not the JSON.org library.
@maerics Edited answer to fit the library requirement, thanks!
Does get return a Number? If it returns Object then doubleValue will not apply.
In which case, this won't actually compile.
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();
|
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

Comments

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

3 Comments

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
@user1720860: note that both the type and the cast must be "Double".
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.