net.sf.json字符串转json对象,数字可能会使用科学计数而损失精度

maven配置:


    net.sf.json-lib
    json-lib
    2.4
    jdk15


    com.google.code.gson
    gson
    2.8.1

测试代码:

package com.skycloud.deviceInfo;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.sf.json.JSONObject;

/**
 * Created by Administrator on 2017/7/13 0013.
 */
public class Test
{
    public static void main(String[] args) {
        String str = "{\"PDisks\":{\"304|C|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\":{\"capable_speed\":8,\"commit_job\":false,\"data_integrity_field\":1,\"device_description\":\"Disk 0 in Backplane 1 of Integrated RAID Controller 1\",\"device_protocol\":\"\",\"enclosures\":\"/sysmgmt/2010/storage/enclosure/308|C|Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"fail_predict\":false,\"firmware_version\":\"TT31\",\"form_factor\":2,\"free_space\":0.0,\"hotspare\":0,\"led\":\"/sysmgmt/2013/storage/led?pdisk_keys=304|C|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"manufactured_day\":4,\"manufactured_week\":41,\"manufactured_year\":2015,\"manufacturer\":\"SEAGATE\",\"media_type\":0,\"model\":\"\",\"name\":\"Physical Disk 0:1:0\",\"negotiated_speed\":8,\"op_state\":65535,\"part_number\":\"CN0WXPCX726225AC01NFA00\",\"pcie_capable_link_width\":0,\"pcie_negotiated_link_width\":0,\"pending_changes\":0,\"power_status\":0,\"product_id\":\"ST1200MM0088\",\"progress\":0,\"protocol\":32,\"remaining_drive_life\":255,\"sasAddress\":\"5000C5008F2E16F9\",\"sector_size\":512,\"sedcapability\":0,\"serial_number\":\"S400TJCX\",\"size\":1199638052864.0,\"slot\":0,\"state\":2,\"status\":2,\"used_space\":1199638052864.0,\"vdisks\":\"/sysmgmt/2010/storage/vdisk?pdisk=304|C|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\"},\"304|C|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\":{\"capable_speed\":8,\"commit_job\":false,\"data_integrity_field\":1,\"device_description\":\"Disk 1 in Backplane 1 of Integrated RAID Controller 1\",\"device_protocol\":\"\",\"enclosures\":\"/sysmgmt/2010/storage/enclosure/308|C|Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"fail_predict\":false,\"firmware_version\":\"TT31\",\"form_factor\":2,\"free_space\":0.0,\"hotspare\":0,\"led\":\"/sysmgmt/2013/storage/led?pdisk_keys=304|C|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"manufactured_day\":4,\"manufactured_week\":41,\"manufactured_year\":2015,\"manufacturer\":\"SEAGATE\",\"media_type\":0,\"model\":\"\",\"name\":\"Physical Disk 0:1:1\",\"negotiated_speed\":8,\"op_state\":65535,\"part_number\":\"CN0WXPCX726225AC01XSA00\",\"pcie_capable_link_width\":0,\"pcie_negotiated_link_width\":0,\"pending_changes\":0,\"power_status\":0,\"product_id\":\"ST1200MM0088\",\"progress\":0,\"protocol\":32,\"remaining_drive_life\":255,\"sasAddress\":\"5000C5008F2E0CA5\",\"sector_size\":512,\"sedcapability\":0,\"serial_number\":\"S400TJF9\",\"size\":1199638052864.0,\"slot\":1,\"state\":2,\"status\":2,\"used_space\":1199638052864.0,\"vdisks\":\"/sysmgmt/2010/storage/vdisk?pdisk=304|C|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\"},\"304|P|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\":{\"capable_speed\":8,\"commit_job\":false,\"data_integrity_field\":1,\"device_description\":\"Disk 0 in Backplane 1 of Integrated RAID Controller 1\",\"device_protocol\":\"\",\"enclosures\":\"/sysmgmt/2010/storage/enclosure/308|P|Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"fail_predict\":false,\"firmware_version\":\"TT31\",\"form_factor\":2,\"free_space\":0.0,\"hotspare\":0,\"led\":\"/sysmgmt/2013/storage/led?pdisk_keys=304|P|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"manufactured_day\":4,\"manufactured_week\":41,\"manufactured_year\":2015,\"manufacturer\":\"SEAGATE\",\"media_type\":0,\"model\":\"\",\"name\":\"Physical Disk 0:1:0\",\"negotiated_speed\":8,\"op_state\":65535,\"part_number\":\"CN0WXPCX726225AC01NFA00\",\"pcie_capable_link_width\":0,\"pcie_negotiated_link_width\":0,\"pending_changes\":0,\"power_status\":0,\"product_id\":\"ST1200MM0088\",\"progress\":0,\"protocol\":32,\"remaining_drive_life\":255,\"sasAddress\":\"5000C5008F2E16F9\",\"sector_size\":512,\"sedcapability\":0,\"serial_number\":\"S400TJCX\",\"size\":1199638052864.0,\"slot\":0,\"state\":2,\"status\":2,\"used_space\":1199638052864.0,\"vdisks\":\"/sysmgmt/2010/storage/vdisk?pdisk=304|P|Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1\"},\"304|P|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\":{\"capable_speed\":8,\"commit_job\":false,\"data_integrity_field\":1,\"device_description\":\"Disk 1 in Backplane 1 of Integrated RAID Controller 1\",\"device_protocol\":\"\",\"enclosures\":\"/sysmgmt/2010/storage/enclosure/308|P|Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"fail_predict\":false,\"firmware_version\":\"TT31\",\"form_factor\":2,\"free_space\":0.0,\"hotspare\":0,\"led\":\"/sysmgmt/2013/storage/led?pdisk_keys=304|P|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\",\"manufactured_day\":4,\"manufactured_week\":41,\"manufactured_year\":2015,\"manufacturer\":\"SEAGATE\",\"media_type\":0,\"model\":\"\",\"name\":\"Physical Disk 0:1:1\",\"negotiated_speed\":8,\"op_state\":65535,\"part_number\":\"CN0WXPCX726225AC01XSA00\",\"pcie_capable_link_width\":0,\"pcie_negotiated_link_width\":0,\"pending_changes\":0,\"power_status\":0,\"product_id\":\"ST1200MM0088\",\"progress\":0,\"protocol\":32,\"remaining_drive_life\":255,\"sasAddress\":\"5000C5008F2E0CA5\",\"sector_size\":512,\"sedcapability\":0,\"serial_number\":\"S400TJF9\",\"size\":1199638052864.0,\"slot\":1,\"state\":2,\"status\":2,\"used_space\":1199638052864.0,\"vdisks\":\"/sysmgmt/2010/storage/vdisk?pdisk=304|P|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1\"}}}";
        JSONObject returnData1 = JSONObject.fromObject(str);
        System.out.println(returnData1);
        JsonObject returnData2 = new JsonParser().parse(str).getAsJsonObject();
        System.out.println(returnData2);
    }
}

结论:

  1. str中的“1199638052864”,在returnData1中为1.19963805E12,在returnData2中为1199638052864.0
  2. 结论1中的情况并不绝对成立。当str长度较小时,数字并不会使用科学计数法。
  3. 并未证明,gson不会使用科学计数法,可能只是str还不够长。这里只是说,当使用json-lib出现该问题时,可以尝试使用gson。

你可能感兴趣的:(net.sf.json字符串转json对象,数字可能会使用科学计数而损失精度)