u010876070
ttvc_all
采纳率50%
2017-05-24 13:07 阅读 4.6k

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条回答 默认 最新

  • 已采纳
    u010876070 ttvc_all 2017-05-25 02:39

    已经解决了,使用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"));
                }
    
    }
    
    点赞 1 评论 复制链接分享
  • u010876070 ttvc_all 2017-05-25 00:59

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

    点赞 1 评论 复制链接分享
  • oSanYeCao1234567 RoidCoder 2017-05-24 14:28

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

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

    点赞 评论 复制链接分享

相关推荐