简体   繁体   English

Java 中的 JSON 加载:java.lang.ClassCastException:java.lang.Long 不能转换为 java.lang.Integer

[英]JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

I'm trying out the example from here , and am using this code to write to a file:我正在尝试这里的示例,并使用此代码写入文件:

    public void Write() {

        FileWriter file = null;
        try {
            JSONObject o = new JSONObject();
            JSONObject obj = new JSONObject();
            obj.put("name", "mky4ong.com");
            obj.put("age", new Integer(100));
            JSONObject obj2 = new JSONObject();
            obj2.put("name", "mk54yong.com");
            obj2.put("age", new Integer(1800));
            file = new FileWriter(filename);
            JSONArray list = new JSONArray();
            list.add(obj);
            list.add(obj2);

            o.put("messages", list);

            file.write(o.toJSONString());
            file.flush();
            file.close();
        } catch (IOException ex) {logger.error("{}", ex.getCause());} finally {try {file.close();} catch (IOException ex) {logger.info("{}",ex.getCause());}}   
}

and using this code to read from the same file:并使用此代码从同一个文件中读取:

public void Load() {
    JSONParser parser = new JSONParser();
    Object obj = null;
    try {
        obj = parser.parse(new FileReader(filename));
    } catch (IOException | ParseException ex) {logger.info("{}", ex.getCause());}
    JSONObject jsonObject = (JSONObject) obj;
    JSONArray msg = (JSONArray) jsonObject.get("messages");
    Iterator<JSONObject> iterator = msg.iterator();
    while (iterator.hasNext()) {
        JSONObject ob = iterator.next();
        String name =(String) ob.get("name");
        Integer age =(Integer) ob.get("age");
        logger.info("name: {}, age: {}", name, age);
    }
}
}

But although the data is written successfully as {"messages":[{"name":"mky4ong.com","age":100},{"name":"mk54yong.com","age":1800}]} , I have trouble while loading.但是虽然数据写入成功为{"messages":[{"name":"mky4ong.com","age":100},{"name":"mk54yong.com","age":1800}]} ,我有麻烦,同时加载。 At this line Integer age =(Integer) ob.get("age");在这一行Integer age =(Integer) ob.get("age"); the compiler says "Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer".编译器说“线程“main”中的异常 java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer”。

I tried casting in multiple ways, but it doesn't work.我尝试以多种方式投射,但它不起作用。 Why does such an error happen?为什么会发生这样的错误?

ps: I'm using compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' ps:我正在使用compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

When you write your json file all additional information is lost (in your case the Integer type you used. When you read it the JSONParser will automatically use Long when it encounters a number without decimal points. Try using Long in your reader. Note that the reader knows nothing about the writer. It can only read a file and interpret it as it sees fit.当您编写 json 文件时,所有附加信息都将丢失(在您的情况下是您使用的Integer类型。当您阅读它时,JSONParser 将在遇到没有小数点的数字时自动使用Long 。尝试在您的阅读器中使用Long 。请注意reader 对 writer一无所知。它只能读取文件并按照它认为合适的方式解释它。

So to answer your question:所以要回答你的问题:

Long age =(Long) ob.get("age");

will work.将工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long - java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long SDN4 java.lang.ClassCastException:使用AttributeConverter时无法将java.lang.Integer强制转换为java.lang.Long - SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter Hibernate HQL强制转换java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Long - Hibernate HQL casting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long java.lang.ClassCastException:无法强制转换java.lang.Integer - java.lang.ClassCastException: java.lang.Integer cannot be cast java.lang.ClassCastException:不能强制转换为java.lang.Long - java.lang.ClassCastException: cannot be cast to java.lang.Long java.lang.ClassCastException:无法转换为java.lang.Integer - java.lang.ClassCastException: cannot be cast to java.lang.Integer java.lang.Integer 不能转换为 java.lang.Long - java.lang.Integer cannot be cast to java.lang.Long 无法将java.lang.Long强制转换为java.lang.Integer - java.lang.Long cannot be cast to java.lang.Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM