java转json科学计数法,如何防止Gson将长数字(json字符串)转换为科学记数法格式?...

I need to convert json string to java object and display it as a long. The json string is a fixed array of long numbers:

{numbers

[ 268627104, 485677888, 506884800 ] }

The code to convert works fine in all cases except for numbers ending in 0. It converts those to a scientific notation number format:

public static Object fromJson(HttpResponse response, Class> classOf)

throws IOException {

InputStream instream = response.getResponseInputStream();

Object obj = null;

try {

Reader reader = new InputStreamReader(instream, HTTP.UTF_8);

Gson gson = new Gson();

obj = gson.fromJson(reader, classOf);

Logger.d(TAG, "json --> "+gson.toJson(obj));

} catch (UnsupportedEncodingException e) {

Logger.e(TAG, "unsupported encoding", e);

} catch (Exception e) {

Logger.e(TAG, "json parsing error", e);

}

return obj;

}

The actual result:

Java object : 268627104, 485677888, 5.068848E+8

Notice the last number is converted to a scientific notation format. Can anyone suggest what could be done to work around it or prevent it or undo it? I'm using Gson v1.7.1

解决方案

If serializing to a String is an option for you, you can configure GSON to do so with:

GsonBuilder gsonBuilder = new GsonBuilder();

gsonBuilder.setLongSerializationPolicy( LongSerializationPolicy.STRING );

Gson gson = gsonBuilder.create();

This will produce something like:

{numbers : [ "268627104", "485677888", "506884800" ] }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值