0

这是我最后一个问题的延续: 如何将数据保存到哈希表中 - java

这是json:

{"DeviceID":"35181805087546548","TotalCount":"0","Timestamp":"2013-03-05 14:30:15"}

这是我的哈希表的声明:

    private static Hashtable<Integer,String> table = new Hashtable<Integer,String>();
    private static AtomicInteger count = new AtomicInteger() ;

这是解析 jsonobject 的代码:

        JSONObject jsonObj;
        try {
            jsonObj = new JSONObject(string);
            int id = jsonObj.optInt("DeviceID", count.addAndGet(1) );
            String name = jsonObj.toString();
            table.put(id, name);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

即使 json 中的“DeviceID”发生变化,使用我的 id 下面的代码始终是“2147483647”。有什么提示吗?

非常感谢

4

3 回答 3

4

Your value is too large for an int. The largest allowable value is 2,147,483,647. See What is the maximum value for an int32?. You will need to parse the value as a long.

于 2013-03-05T20:42:58.060 回答
1

The maximum value of an integer in Java is 2,147,483,647, so your device ID doesn't fit. You will probably need to store it as a long (maximum value 9,223,372,036,854,775,807).

于 2013-03-05T20:43:33.863 回答
1

The number you are getting is the maximum value of a signed integer, you need to use long, or your result is limited to Integer.MAX_VALUE.

于 2013-03-05T20:43:57.700 回答