Skip to content

[BUG] 从数组类对象中根据jsonpath读取值,fastjson1能直接从数组元素对象中读取,相同的jsonpath在fastjson2无法读取 #3331

@JHWang-1997

Description

@JHWang-1997

问题描述

对于JSONArray对象,
fastjson1中可以直接从json中读取到数组元素内部的字段,fastjson2则不行,造成不兼容

环境信息

请填写以下信息:

  • OS信息:
  • JDK信息: Openjdk 1.8.0_312
  • 版本信息:fastjson2 2.0.54

重现步骤

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

//可在此输入示例代码
    public static void main(String[] args) throws JsonProcessingException {
        String msg = "[{\"priority_flag\":9,\"service_type\":\"bbb\",\"destination_addr\":\"bbb\"}]";
        Object parse1 = com.alibaba.fastjson.JSON.parse(msg);
        boolean contains1 = com.alibaba.fastjson.JSONPath.contains(parse1, "$.service_type");
        System.out.println(contains1); // true
        Object parse2 = com.alibaba.fastjson2.JSON.parse(msg);
        boolean contains2 = com.alibaba.fastjson2.JSONPath.contains(parse2, "$.service_type");
        System.out.println(contains2);  // false
    }

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
fastjson1的com.alibaba.fastjson.JSONPath#getPropertyValue会依次判断入参对象是否为String,Map或List,如果是List则会遍历其元素进行取值。
而fastjson2中类似$.service_type的jsonpath只会使用JSONPathSingleName判断,仅支持Map类型对象。
请问这是特性如此还是不兼容bug

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

使用标准的数组jsonpath,fastjson1和fastjson2都没有问题

    public static void main(String[] args) throws JsonProcessingException {
        String msg = "[{\"priority_flag\":9,\"service_type\":\"bbb\",\"destination_addr\":\"bbb\"}]";
        Object parse1 = com.alibaba.fastjson.JSON.parse(msg);
        boolean contains1 = com.alibaba.fastjson.JSONPath.contains(parse1, "$[0].service_type");
        System.out.println(contains1); // true
        Object parse2 = com.alibaba.fastjson2.JSON.parse(msg);
        boolean contains2 = com.alibaba.fastjson2.JSONPath.contains(parse2, "$[0].service_type");
        System.out.println(contains2);  // true
    }

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