Skip to content
New issue

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

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

Already on GitHub? # to your account

com.alibaba.fastjson2.JSONObject#toJavaObject(java.lang.Class<T>, com.alibaba.fastjson2.JSONReader.Feature...)bug #2632

Closed
fun-li opened this issue May 28, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@fun-li
Copy link

fun-li commented May 28, 2024

问题描述

该方法在开启JSONReader.Feature.SupportSmartMatch模式后,只能解析最外层的下划线变量,内层无法解析到,例如下面的dept_order_list中的dept_id获取到的值为空

{
  "errcode": 0,
  "errmsg": "ok",
  "result": {
    "active": true,
    "admin": false,
    "avatar": "",
    "boss": false,
    "create_time": "2024-05-14T08:05:45.000Z",
    "dept_id_list": [
      111111111
    ],
    "dept_order_list": [
      {
        "dept_id": 111111111,
        "order": 1111111111111
      }
    ],
    "email": "",
    "exclusive_account": false,
    "hide_mobile": false,
    "leader_in_dept": [
      {
        "dept_id": 862258146,
        "leader": false
      }
    ],
    "mobile": "11111111111",
    "name": "张三",
    "real_authed": false,
    "remark": "",
    "senior": false,
    "state_code": "86",
    "telephone": "",
    "title": "开发部",
    "unionid": "IKKo8dfdfdfdfiSfn2nJEOwiEiE",
    "userid": "zhangsan",
    "work_place": ""
  },
  "request_id": "16khdfdfsdfkv560dw"
}

bean对象:

@Data
public class DDUserInfo {

    /**
     * The extension field, represented as a String.
     */
    private String extension;

    /**
     * The unionid field, represented as a String.
     */
    private String unionid;

    /**
     * The boss field, represented as a String.
     */
    private String boss;

    /**
     * A list of roles associated with the user.
     */
    private List<Role> roleList;

    /**
     * A boolean indicating whether the account is exclusive.
     */
    private boolean exclusiveAccount;

    /**
     * The manager's user id, represented as a String.
     */
    private String managerUserid;

    /**
     * The admin field, represented as a String.
     */
    private String admin;

    /**
     * Any remarks associated with the user, represented as a String.
     */
    private String remark;

    /**
     * The title of the user, represented as a String.
     */
    private String title;

    /**
     * The date the user was hired, represented as a String.
     */
    private String hiredDate;

    /**
     * The user's id, represented as a String.
     */
    private String userid;

    /**
     * The user's workplace, represented as a String.
     */
    private String workPlace;

    /**
     * The order of the employee in the corresponding department.
     */
    private List<DeptOrder> deptOrderList;

    /**
     * A String indicating whether the user is authed in real life.
     */
    private String realAuthed;

    /**
     * A list of department ids associated with the user, represented as a String.
     */
    private String deptIdList;

    /**
     * The user's dept, represented as a String.
     */
    private String deptName;

    /**
     * The user's job number, represented as a String.
     */
    private String jobNumber;

    /**
     * The user's email, represented as a String.
     */
    private String email;

    /**
     * Information about the user's position in the department and whether they are a leader.
     */
    private List<LeaderInDept> leaderInDept;

    /**
     * The user's mobile number, represented as a String.
     */
    private String mobile;

    /**
     * A String indicating whether the user is active.
     */
    private String active;

    /**
     * The user's organization email, represented as a String.
     */
    private String orgEmail;

    /**
     * The user's telephone number, represented as a String.
     */
    private String telephone;

    /**
     * The user's avatar, represented as a String.
     */
    private String avatar;

    /**
     * A String indicating whether the user's mobile number is hidden.
     */
    private String hideMobile;

    /**
     * A String indicating whether the user is a senior.
     */
    private String senior;

    /**
     * The user's name, represented as a String.
     */
    private String name;

/**
 * Extended information about the user's union membership.
 */
// private UnionEmpExt unionEmpExt;

    /**
     * The user's state code, represented as a String.
     */
    private String stateCode;

    @Data
    public static class LeaderInDept {
        private String leader;
        private Long deptId;
    }

    @Data
    public static class DeptOrder {
        private Long deptId;
        private String order;
    }

    @Data
    public static class Role {
        private String groupName;
        private String name;
        private Long id;
    }
}

环境信息

请填写以下信息:

  • OS信息: win11
  • JDK信息: jdk 1.8.0_321]
  • 版本信息:Fastjson2 2.0.50

重现步骤

   public static void main(String[] args) {
        String jsn = "{\n" +
                "  \"errcode\": 0,\n" +
                "  \"errmsg\": \"ok\",\n" +
                "  \"result\": {\n" +
                "    \"active\": true,\n" +
                "    \"admin\": false,\n" +
                "    \"avatar\": \"\",\n" +
                "    \"boss\": false,\n" +
                "    \"create_time\": \"2024-05-14T08:05:45.000Z\",\n" +
                "    \"dept_id_list\": [\n" +
                "      111111111\n" +
                "    ],\n" +
                "    \"dept_order_list\": [\n" +
                "      {\n" +
                "        \"dept_id\": 111111111,\n" +
                "        \"order\": 1111111111111\n" +
                "      }\n" +
                "    ],\n" +
                "    \"email\": \"\",\n" +
                "    \"exclusive_account\": false,\n" +
                "    \"hide_mobile\": false,\n" +
                "    \"leader_in_dept\": [\n" +
                "      {\n" +
                "        \"dept_id\": 862258146,\n" +
                "        \"leader\": false\n" +
                "      }\n" +
                "    ],\n" +
                "    \"mobile\": \"11111111111\",\n" +
                "    \"name\": \"张三\",\n" +
                "    \"real_authed\": false,\n" +
                "    \"remark\": \"\",\n" +
                "    \"senior\": false,\n" +
                "    \"state_code\": \"86\",\n" +
                "    \"telephone\": \"\",\n" +
                "    \"title\": \"开发部\",\n" +
                "    \"unionid\": \"IKKo8dfdfdfdfiSfn2nJEOwiEiE\",\n" +
                "    \"userid\": \"zhangsan\",\n" +
                "    \"work_place\": \"\"\n" +
                "  },\n" +
                "  \"request_id\": \"16khdfdfsdfkv560dw\"\n" +
                "}";
        JSONObject o = JSON.parseObject(jsn);
        JSONObject result = o.getJSONObject("result");
        DDUserInfo javaObject = result.toJavaObject(DDUserInfo.class, JSONReader.Feature.SupportSmartMatch);
        System.out.println(javaObject);
    }
@fun-li fun-li added the bug Something isn't working label May 28, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 30, 2024
@wenshao
Copy link
Member

wenshao commented May 30, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证

@wenshao wenshao added the fixed label May 30, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

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

@wenshao wenshao closed this as completed Jun 1, 2024
@fun-li
Copy link
Author

fun-li commented Jun 1, 2024

谢谢大佬

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

No branches or pull requests

2 participants