java上使用Gson转换长数字的字符串时,被当成了数字处理

直接上代码
public static void main(String[] args) {
String jsonString="{\"list\":[{\"Arrayname\": \"000292603088\",\"LUNID\": \"09FB\",\"Capacity\": \"134.8681640625\",\"UUID\": \"60000970000292603088533030394642\",\"Mode\": \"vmax\"},"
+ "{ \"Arrayname\": \"000292603088\",\"LUNID\":\"09EB\",\"Capacity\":\"134.8681640625\",\"UUID\": \"60000970000292603088533030394542\",\"Mode\":\"vmax\"}]}";
Gson gson = new Gson();

     Map<String, Object > itemmapMap=gson.fromJson(jsonString, Map.class);

        String tolist= itemmapMap.get("list").toString();
        System.out.println("原始信息:"+tolist);

    List<Map<String, Object>> listjson =gson.fromJson(tolist, List.class);
            for (int i = 0; i < listjson.size(); i++) {
                    Map<String, Object> itemMap=new HashMap<String, Object>();
                    itemMap=listjson.get(i);
                    Map<String, String> insertMap=new HashMap<String, String>();

                    insertMap.put("lunId", itemMap.get("UUID").toString());



                System.out.println("转换成了:"+insertMap.get("lunId"));
            }

}
原始信息:[{Arrayname=000292603088, LUNID=09FB, Capacity=134.8681640625, UUID=60000970000292603088533030394642, Mode=vmax}, {Arrayname=000292603088, LUNID=09EB, Capacity=134.8681640625, UUID=60000970000292603088533030394542, Mode=vmax}]

转换成了:6.00009700002926E31
转换成了:6.00009700002926E31

请大神指点,希望数据变成60000970000292603088533030394542,而不是6.00009700002926E31
请问gson是否能指定字段的转换类型

3个回答

已经解决了,使用JSONObject来实现了

public static void main(String[] args) {
    String jsonString="{\"list\":[{\"Arrayname\": \"000292603088\",\"LUNID\": \"09FB\",\"Capacity\": \"134.8681640625\",\"UUID\": \"60000970000292603088533030394642\",\"Mode\": \"vmax\"},"
            + "{ \"Arrayname\": \"000292603088\",\"LUNID\":\"09EB\",\"Capacity\":\"134.8681640625\",\"UUID\": \"60000970000292603088533030394542\",\"Mode\":\"vmax\"}]}";

// tolist=gson.toJson(tolist);
JSONObject jsonObject=JSONObject.fromObject(jsonString);
JSONArray listArray= jsonObject.getJSONArray("list");

            for (int i = 0; i < listArray.size(); i++) {
                    Map<String, String> insertMap=new HashMap<String, String>();

                    insertMap.put("lunId", listArray.getJSONObject(i).optString("UUID"));



                System.out.println("转换成了:"+insertMap.get("lunId"));
            }

}

这个是别人给的接口数据,没法改的

\"UUID\": \"60000970000292603088533030394542\" 不要把他作为String存在json字符串中,可以这样:

\"UUID\": 60000970000292603088533030394542 把引号去掉即可

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐