• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JSON java.lang.Integer cannot be cast to java.lang.Long

 
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I’m completely stuck with an issue.
There is now some days that I'm struggling about that issue :-(
I need really your help :-)

Same code, 2 different behaviors.
I am using json-simple
I have tested several approaches without success.
I know that JSON is returning a Long object.
But, even with that, why  in "Approach 1" the code is working, and not in "Approach 2" ?

With the following code



I get:

APPROACH  1
---> Construct JSON data
{"enthalpyBkgdImg":{"refCurveH1x":140,"enthalpyImageFile":"R22_A4.png"},"nameRefrigerant":"R22","xHmin":140.0}

---> Set the Class Instance with JSON data
{"enthalpyBkgdImg":{"refCurveH1x":140,"enthalpyImageFile":"R22_A4.png"},"nameRefrigerant":"R22","xHmin":140.0}

APPROACH  2
---> Construct JSON data
{"enthalpyBkgdImg":{"refCurveH1x":140,"enthalpyImageFile":"R22_A4.png"},"nameRefrigerant":"R22","xHmin":140.0}

---> Set the Class Instance with JSON data
{"enthalpyBkgdImg":{"refCurveH1x":140,"enthalpyImageFile":"R22_A4.png"},"nameRefrigerant":"R22","xHmin":140.0}
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at pacp.test.HelpJSON$EnthalpyBkgdImg.setJsonObject(HelpJSON.java:93)
at pacp.test.HelpJSON$Enthalpy.setJsonObject(HelpJSON.java:144)
at pacp.test.HelpJSON.main(HelpJSON.java:47)


ISSUE is located on

When I replace that code with


Vice versa "APPROACH 2" is working and not "APPROACH 1"

I become crazy...
How to fix that ?
Many thanks in advance

Regards

Christian

 
Marshal
Posts: 4796
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since refCurveH1x is a numeric attribute, jsonObj.get("refCurveH1x") probably returns a type of Number.

If that is true, then you should be able to do: this.refCurveH1x = ((Number) jsonObj2.get("refCurveH1x")).intValue(); if you want to interpret the value as an int.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks this is working... :-)

Nevertheless where is the logic
I read that:
The abstract class Number is the  superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and short.
as int refCurveH1x; so this.refCurveH1x = ((Number) jsonObj2.get("refCurveH1x")).intValue(); is OK
I'm using  
double xHmin; so normally I should also have the right to cast to "(Number)", for a same approach to JSON .get, but compiler doesn't accept
There is something that I didn't catch..
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like:
this.xHmin = (Number) jsonObj.get("xHmin");

--> not accepted
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really sorry, My last answer has been sent to quickly ..

Answer was of course:

Which is the logical code approach

Many many thanks
 
Ron McLeod
Marshal
Posts: 4796
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Klugesherz wrote:like:
this.xHmin = (Number) jsonObj.get("xHmin");

--> not accepted


How about this?: this.xHmin = ((Number) jsonObj.get("xHmin")).doubleValue();

Edit: looks like you figured it out while I was typing
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes !
Thanks Ron
 
reply
    Bookmark Topic Watch Topic
  • New Topic