Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]class com.alibaba.fastjson2.JSONObject cannot be cast to class com.alibaba.fastjson.JSONObject (com.alibaba.fastjson2.JSONObject and com.alibaba.fastjson.JSONObject are in unnamed module of loader 'app') #2673

Closed
gavinxzw opened this issue Jun 6, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@gavinxzw
Copy link

gavinxzw commented Jun 6, 2024

问题描述

简要描述您碰到的问题。
使用兼容1.x进行javabean转换时出现不是预期的
com.alibaba.fastjson.JSONObject
而是变成com.alibaba.fastjson2.JSONObject
请看代码例子

环境信息

请填写以下信息:

  • OS信息:
  • JDK信息: jdk17
  • 版本信息:Fastjson2.0.51

重现步骤

如何操作可以重现该问题:

  1. 使用 xxx.xxx 方法
  2. 输入 ... 数据
  3. 出现 ... 错误
//可在此输入示例代码

package com.efuture.ocp.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class JsonToBean {

public static void main(String[] args) {
    String jsonStr = "{\r\n"
            + "  \"id\":\"01\",\r\n"
            + "  \"data\":{\r\n"
            + "      \"key\":\"test\"\r\n"
            + "  }\r\n"
            + "}";
     JSONObject json = JSON.parseObject(jsonStr);
     Bean bean = JSON.toJavaObject(json, Bean.class);
     JSONObject o = (JSONObject)bean.getData();
    
}
class Bean {
    private String id;
    private Object data;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    
}

}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
期望的data应该解释成com.alibaba.fastjson.JSONObject

相关日志输出

请复制并粘贴任何相关的日志输出。
Exception in thread "main" java.lang.ClassCastException: class com.alibaba.fastjson2.JSONObject cannot be cast to class com.alibaba.fastjson.JSONObject (com.alibaba.fastjson2.JSONObject and com.alibaba.fastjson.JSONObject are in unnamed module of loader 'app')
at com.efuture.ocp.common.JsonToBean.main(JsonToBean.java:17)

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@gavinxzw gavinxzw added the bug Something isn't working label Jun 6, 2024
@wenshao wenshao added this to the 2.0.52 milestone Jun 6, 2024
@wenshao
Copy link
Member

wenshao commented Jun 7, 2024

这个兼容处理起来有点麻烦,要慢慢想办法哈

rowstop added a commit to rowstop/fastjson2 that referenced this issue Jun 13, 2024
wenshao pushed a commit that referenced this issue Jun 13, 2024
)

* Improve the compatibility of fastjson1-compatible for issue #2673
@wenshao wenshao added the fixed label Jun 13, 2024
@wenshao
Copy link
Member

wenshao commented Jul 14, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.52
问题已修复,请用新版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants