JSON - simple get an Integer instead of Long - java

How to get an Integer instead of Long from JSON?
I want to read JSON in my Java program, but when I get a JSON value which is a number, my parser returns a number of type Long.
I want to get an Integer. I tried to cast the long to an integer, but java throws a ClassCastException (java.lang.Long cannot be cast to java.lang.Integer).
I tried several things, such as first converting the long to a string, and then converting with Integer.parseInt(); but also that doesn't work.
I am using json-simple
Edit:
I still can't get it working. Here is an example:
jsonItem.get("amount"); // returns an Object
I can do this:
(long)jsonItem.get("amount");
But not this:
(int)jsonItem.get("amount");
I also can't convert it with
Integer newInt = new Integer(jsonItem.get("amount"));
or
Integer newInt = new Integer((long)jsonItem.get("amount"));

Please understand that Long and Integer are object classes, while long and int are primitive data types. You can freely cast between the latter (with possible loss of high-order bits), but you must do an actual conversion between the former.
Integer newInt = new Integer(oldLong.intValue());

I tried
(int)(long)jsonItem.get("amount");
and it worked for me

Related

Efficient way to convert long to String in Java

I've been using
long a = 123456789;
String b = a+"";
to convert a long value (or int) to String, or in this perspective, treat it as String. My question is, is it ok to do this? Are there any negative impact?
And is there any difference in using String.valueOf() vs Long.toString()?
Thanks
It is ok to do this as recent JVM will likely reduce it to:
String b = String.valueOf(a);
As for negatives, it is not good Java coding style as there is ambiguity. If a was null, would b = "null"? or will an NPE be thrown? You know the answer with experience, but this should be obvious to all readers of your code.
First, your code doesn't compile - you have to append an L after the literal:
Long a = 123456789L;
String.valueOf() and Long.toString() methods both have a primitive long as a parameter, but since a is an wrapper (Long), you can just use the Object#toString() method:
String b = a.toString();
If a, however, is a primitive (long), both String.valueOf() and Long.toString() will do the same, so it's a matter of preference which one to use.

How to convert String value into generic type

We have a class named as Show.
Can anybody tell me how to convert data which is String into generic type? For example if we have
String sn=null;
and we want to cast it into Show then how we can do it?
your question is really very unclear. but if you are talking about converting data in String format to primitive data types then you can use parser. like if you want to convert "9" which is basically a string to int then you can use
int i = Integer.parseInt("9");

Type conversion in Java (Double / double)

Please explain how type conversion in Java (Double / double, Integer / integer, ...) works.
Why is only the last example valid?
setLocation(double, double);
// This don't work
player.setLocation((Double) jsonMsg.get("x"), (Double) jsonMsg.get("y"));
// This don't work too
player.setLocation((double) jsonMsg.get("x"), (double) jsonMsg.get("y"));
// It's ok!
player.setLocation( Double.parseDouble(jsonMsg.get("x").toString())
, Double.parseDouble(jsonMsg.get("y").toString())
);
Seems that your jsonMsg.get("x") returns an object which string representation can be converted to a double type value.
And your setLocation method needs two double type parameters, i.e declared as
setLocation(double a, double b)
To convert strings to double you need to invoke Double.parseDouble(). It parses parameter string to a double value and returns it.
UPDATE:
in java it is not possible to cast an object variable to a primitive type. So you need to invoke a parsing method of a Double class to convert this string object to a double type value.
Java do type conversion implicitly between Wrapper class(Integer,...) and Primitive type (int,...) after Java 1.5.
but what you're trying to do in the first two steps is to cast Object/String into Double which is not allowed and need explicit manipulation like Double.parseDouble(jsonMsg.get("x").toString()) or new Double("1.2").
Thanks
Notice: String is a object and if you want to convert object type to primitive type you have to use wrapper/covering classes. You cannot do casting.
Java has wrapper classes for every primitive data types. As a example:
int   - Integer
double - Double
Wrapper classes have methods to manipulate primitive data types and to convert objects to primitive data types. As a example:
For int - Integer.parseInt()
For double - Double.parseDouble()
In your code snippet last line:
Double.parseDouble(jsonMsg.get("x").toString()
you are parsing String to double it is correct and in other two line you are trying to cast. In one using wrapper class:
player.setLocation((Double) jsonMsg.get("x"), (Double) jsonMsg.get("y"));
and other one using primitve type:
player.setLocation((double) jsonMsg.get("x"), (double) jsonMsg.get("y"));
Both are incorrect. Because of jsonMsg.get("x") returns String object. You have to convert it into String using .toString() and parse it to double.
Notice: toString() method is used when we need a string representation of an object.
I hope you get the point.

org.json.JSONObject messing with data

I got a different type of numbers in my json string. So parsing this numbers with JSONObject leads to 3.7E-4-like representation of this numbers. I prefer to see numbers as a string. What to do? How to prevent such conversion?
{"data":
{"number1":0.0004,
"number2":0.00038,
"number3":0.00037
}}
Simply, create a string before putting your number to JSON.
or
int number = 0;
json.put(number + "");
Can you give an example number, not represented like above?
I think it is some limitation of this particular json library. As a workaround, you could convert parsed values to BigDecimal and use it, unless the double conversion does not lose precision significantly.
For more details read this: How to prevent JSONObject from json.jar converts decimal numbers string into double

not able to convert bigdecimal to string in java

Here is the java code
usageType = (String) c.getSrcValue("USAGETYPE");
c is a arraylist.
I populate it with this field from DB.
"USAGETYPE" NUMBER(*,0),
I get the following error
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to String
Can you please help me out
Well, you cannot convert an object to a string by casting. Not in Java, in any case.
Try
usageType = c.getSrcValue("USAGETYPE").toString();
That is, if you actually need it as a string, which smells a little dubious in the first place. Usually the only place where numbers are needed as strings is the UI and you've got appropriate other places to do that conversion, normally (e.g. CellRenderers in Swing).
Simply write
usageType = c.getSrcValue("USAGETYPE").toString();
or
usageType = ""+c.getSrcValue("USAGETYPE");

Categories