0
我有一個問題顯示值爲浮動序列化/反序列化與json與gson。相反,它會刪除浮點數,而不是顯示fx。 745.0顯示745.任何想法爲什麼?parseFloat gson json字符串
public String execute(HttpServletRequest request, HttpServletResponse response, CurrencyClient client) {
String currency = client.findAll_JSON(String.class);
Gson gson = new Gson();
Currency[] currencies = gson.fromJson(currency, Currency[].class);
System.out.println("currencies " + currencies[0].getRate());
response.setContentType("application/json;charset=UTF-8");
try (PrintWriter pw = response.getWriter()) {
//System.out.println("JSON " + currencies);
MyObject myObject = new MyObject();
for (Object c : currencies) {
myObject.add((gson.toJson(c)));
}
System.out.println("value of rate: " + gson.toJson(myObject.getCurrencies()));
pw.println(gson.toJson(myObject));
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
return super.execute(request);
}
}
類爲MyObject {
ArrayList<Object> currencies = new ArrayList<Object>();
public MyObject() {
}
public void add(Object e){
this.currencies.add(e);
}
public Object getCurrencies() {
return currencies;
}
public void setCurrencies(ArrayList<Object> currencies) {
this.currencies = currencies;
}
和客戶端的代碼:: 這裏正在通過命令圖案產生的後端代碼
$(document).ready(function() {
$.ajax({
url: "Controller",
cache: false,
dataType: "json",
data: {command: 'getCurrencyRates'},
success: function(data) {
$.each(data.currencies, function(index, value) {
var v = JSON.parse(value);
console.log(rate);
$("<tr><td>" + v.code + "</td><td>" + v.description + "</td><td>" + v.rate + "</td></tr>")
.appendTo($("table"));
});
}
});
});
我只能使用v.rate.toFixed(2) –