public class Statics {
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double sum;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double avg;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double max ;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double min ;
private Long count = Long.valueOf(0);
}
Using this annotation: @JsonFormat(shape = JsonFormat.Shape.STRING)
Solved my problem!
public class Statics {
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double sum;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double avg;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double max ;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double min ;
private Long count = Long.valueOf(0);
}
Using this annotation: @JsonFormat(shape = JsonFormat.Shape.STRING)
Solved my problem!
Use String instead of Double do your calculations on double the convert value to String.ValueOf(sum) and set value to your statistics POJO.
Convert your POJO like below
public class Statics {
private String sum;
private String avg;
private String max;
private String min;
private Long count = Long.valueOf(0);//getter and setters and constructrures
}
Its in real not getting converted into int. Only thing happening is as JS Object its not showing .0 which is not relevant.
In your sample program, change some of the value from double[] d = new double[]{1.0,2.0,3.0} to
double[] d = new double[]{1.0,2.1,3.1} and run the program.
You will observer its in real not converting into int. The output you will get is {"doubles":[1,2.1,3.1]}
All numbers are floats in Javascript. So, 1.0 and 1 are the same in JS. There is no differenciation of int, float and double.
Since JSON is going to end up as a JS object, there is no use in adding an extra '.0' since '1' represents a float as well. I guess this is done to save a few bytes in the string that is passed around.
So, you will get a float in JS, and if you parse it back to Java, you should get a double. Try it.
In the mean time, if you are interested in the way it displays on screen, you can try some string formatting to make it look like '1.0'.
Videos
"{"unitfactor":"0.1","unit":"N"}"
its because your unitfactor 0.1 is String in the making. change it to this:
"{"unitfactor":0.1,"unit":"N"}"
Remove the double quotation mark on 0.1.
or try to use the approach of @iNan:
double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor"));
In this approach, you will first get the String unitfactor value, which is 0.1, and then you will parse it to Double using its wrapper class.
Since unitfactor is of type string, If you cannot change JSON then you can use
double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor"))
If you are using Jackson you can use @JsonSerialize and ToStringSerializer :
public MyBean {
@JsonSerialize(using = ToStringSerializer.class)
private double myDouble;
//getter and setter
//constructors
}
The code to test it (Jackson version 2.9.8) :
MyBean myBean = new MyBean(20.3);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(myBean);
System.out.println(json);
The output is :
{"myDouble":"20.3"}
You could either use @JsonSerialize with ToStringSerializer:
@Data
public class MyBean {
@JsonSerialize(using = ToStringSerializer.class)
private double myDouble;
}
Or use @JsonFormat with JsonFormat.Shape.STRING:
@Data
public class MyBean {
@JsonFormat(shape = Shape.STRING)
private double myDouble;
}
Both approaches produce the same result.
If you intend to use these annotations in multiple places, you may consider @JacksonAnnotationsInside which allows you to create a custom annotation which contains one or more Jackson annotations:
@Retention(RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = ToStringSerializer.class)
public @interface JsonString {
}
@Data
public class MyBean {
@JsonString
private double myDouble;
}
There is a built-in method to convert a JSONObject to a String. Why don't you use that:
JSONObject json = new JSONObject();
json.toString();
You can use:
JSONObject jsonObject = new JSONObject();
jsonObject.toString();
And if you want to get a specific value, you can use:
jsonObject.getString("msg");
or Integer value
jsonObject.getInt("codeNum");
For long Integers
jsonObject.getLong("longNum");
You can create a custom Deserializer to deserialize the Message text into Message object and annotate the Message class with @JsonDeserialize:
@JsonDeserialize(using = MessageDeserializer.class)
public class Message {
String language;
String data;
}
public class MessageDeserializer extends JsonDeserializer<Message> {
public MessageDeserializer() {
super();
}
@Override
public Message deserialize(
final JsonParser jsonParser, final DeserializationContext deserializationContext) throws
IOException, JsonProcessingException {
final String messageText = jsonParser.getText();
// parse messageText into Message object
}
}
I am not sure my solution is acceptable since it does require additional explicit call to ObjectMapper to perform deserialization of the string value of Message.
However, this is it is done during the buildup of Notification object and does not require a String message property.
You need to add a ctor with String argument to Message class, where you can deserialize the String into Map and extract the instance propertieds:
public Message(String str) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> map =
(Map<String, Object>)new ObjectMapper().readValue(str, Map.class);
language = map.containsKey("language") ? map.get("language").toString() : null ;
data = map.containsKey("data") ? map.get("data").toString() : null ;
} catch (Exception e) {
e.printStackTrace();
}
}
the new ctor will be called by Jackson when you deserialize a Notification object:
Notification n = (Notification)new ObjectMapper().readValue(reader, Notification.class);
double total = 44;
String total2 = String.valueOf(total);
This will convert double to String
Using Double.toString(), if the number is too small or too large, you will get a scientific notation like this: 3.4875546345347673E-6. There are several ways to have more control of output string format.
double num = 0.000074635638;
// use Double.toString()
System.out.println(Double.toString(num));
// result: 7.4635638E-5
// use String.format
System.out.println(String.format ("%f", num));
// result: 0.000075
System.out.println(String.format ("%.9f", num));
// result: 0.000074636
// use DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
String numberAsString = decimalFormat.format(num);
System.out.println(numberAsString);
// result: 0.000075
Use String.format() will be the best convenient way.
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.
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