fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG]fastjson2 JSONObject.from("test")报错

Open litao11046 opened this issue 1 year ago • 1 comments

问题描述

fastjson2的如下代码报错: JSONObject.from("test");

环境信息

  • 版本信息:[e.g.:Fastjson2 2.0.48]

重现步骤

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

  1. 使用 JSONObject.from 方法
  2. 出现 java.lang.ClassCastException: class java.lang.String cannot be cast to class com.alibaba.fastjson2.JSONObject (java.lang.String is in module java.base of loader 'bootstrap'; com.alibaba.fastjson2.JSONObject is in unnamed module of loader 'app') at com.alibaba.fastjson2.JSONObject.from(JSONObject.java:2028) 错误
JSONObject.from("test");

期待的正确结果

这个问题出现的场景是,传给JSONObject.from的参数值是Object类型,该类型是动态的,可能是基本类型,也可能是包含字段的类型

litao11046 avatar Apr 08 '24 17:04 litao11046

这不是一个JSONObject格式的数据,可以使用 JSON.toJSON,这个方法可能返回 JSONObject、JSONArray 或其它类型

rowstop avatar Apr 10 '24 09:04 rowstop