Skip to content

[BUG] 2.0.52 JSONArray ClassCastException。使用 fastjson1 的 JSON.toJSON() 方法,转换后的数据中存在 com.alibaba.fastjson2.JSONArray 对象。 #2856

@zhangbody

Description

@zhangbody

问题描述

使用 fastjson1 的 JSON.toJSON() 方法,转换后的数据中存在 com.alibaba.fastjson2.JSONArray 对象。

环境信息

请填写以下信息:

  • OS信息:Windows 10
  • JDK信息:JDK信息:Oracle Java 1.8.0_381
  • 版本信息:Fastjson2 2.0.52

重现步骤

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

抛出异常,见日志输出。

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

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

/**
 * JSON 处理方法
 * 
 * @author zhangrong
 * @date 2024/08/07
 */
public class Test {

  public static void main(String[] args) {
    Map map1 = new HashMap();

    List list1 = new LinkedList();
    map1.put("list1", list1);

    Map map2 = new HashMap();
    list1.add(map2);

    List list2 = new LinkedList();
    map2.put("list2", list2);

    Map map3 = new HashMap();
    list2.add(map3);

    List list3 = new LinkedList();
    map3.put("list3", list3);

    list3.add("str1");
    list3.add("str2");
    list3.add("str3");

    Object o1 = JSON.toJSON(map1);

    System.out.println(((JSONArray)((JSONObject)o1).get("list1")).get(0));
  }

}

期待的正确结果

使用 fastjson1 兼容 api 时,返回 fastjson1 的 JSONArray。

相关日志输出

Exception in thread "main" java.lang.ClassCastException: com.alibaba.fastjson2.JSONArray cannot be cast to com.alibaba.fastjson.JSONArray

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions