Spring Boot 使用相同字段名称的JSON到Java反序列化

mwecs4sa  于 2023-06-22  发布在  Spring
关注(0)|答案(2)|浏览(159)

bounty将在5天内到期。回答此问题可获得+100声望奖励。user1578872正在寻找一个规范答案

我在顶级文档中有id,在子文档中有id。未填充子文档ID。
我的JSON文档:-

{
  "_id": "a392ec9c-4f5c-411a-8599-928c5ce6b18d",
  "name": "polName",
  "rules": [
    {
      "t": "TestRule",
      "principals": [
        {
          "name": "Test Report",
          "id": "Test Report",
          "type": "PY"
        },
        {
          "name": "Test Report1",
          "id": "Test Report1",
          "type": "SY"
        }
      ]
    }
  ]
}

Dto类,

@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PolicyDto {

   
    @JsonProperty("docId")
    @JsonAlias({ "_id" })
    private String id;
    private String name;
    private List<Rule> rules;

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public List<Rule> getRules() {
        return rules;
    }

    public void setRules(final List<Rule> rules) {
        this.rules = rules;
    }

    public static class Rule {
        private List<Principal> principals;

        public List<Principal> getPrincipals() {
            return principals;
        }

        public void setPrincipals(final List<Principal> principals) {
            this.principals = principals;
        }

    }

    public static class Principal {
        private String name;
        private String id;
        private String type;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(final String id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(final String type) {
            this.type = type;
        }

    }

}

在这里,principal内部的id被正确填充。

htrmnn0y

htrmnn0y1#

我尝试了以下代码段:

public class MainClass {

    public static void main(String[] args) {
        String json = "{\n"
                + "  \"_id\": \"a392ec9c-4f5c-411a-8599-928c5ce6b18d\",\n"
                + "  \"name\": \"polName\",\n"
                + "  \"rules\": [\n"
                + "    {\n"
                + "      \"t\": \"TestRule\",\n"
                + "      \"principals\": [\n"
                + "        {\n"
                + "          \"name\": \"Test Report\",\n"
                + "          \"id\": \"Test Report\",\n"
                + "          \"type\": \"PY\"\n"
                + "        },\n"
                + "        {\n"
                + "          \"name\": \"Test Report1\",\n"
                + "          \"id\": \"Test Report1\",\n"
                + "          \"type\": \"SY\"\n"
                + "        }\n"
                + "      ]\n"
                + "    }\n"
                + "  ]\n"
                + "}";
        try {
            PolicyDto p = new ObjectMapper().readValue(json, PolicyDto.class);
            System.out.println("P "+p.getRules().get(0).getPrincipals().get(0).toString());
        } catch (JsonProcessingException p) {
            p.printStackTrace();
        }
    }
}

以及DTO:

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
 *
 * @author asgar
 */
@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PolicyDto {

    @JsonProperty("docId")
    @JsonAlias({"_id"})
    private String id;

    private String name;

    private List<Rule> rules;

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public List<Rule> getRules() {
        return rules;
    }

    public void setRules(final List<Rule> rules) {
        this.rules = rules;
    }

    public static class Rule {

        private String t;

        private List<Principal> principals;

        public List<Principal> getPrincipals() {
            return principals;
        }

        public void setPrincipals(final List<Principal> principals) {
            this.principals = principals;
        }

        public String getT() {
            return t;
        }

        public void setT(String t) {
            this.t = t;
        }

    }

    public static class Principal {

        private String name;
        private String id;
        private String type;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(final String id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(final String type) {
            this.type = type;
        }

        @Override
        public String toString() {
            return "Principal{" + "name=" + name + ", id=" + id + ", type=" + type + '}';
        }

    }
}

输出:

P Principal{name=Test Report, id=Test Report, type=PY}

代码工作正常,上面提到的id似乎也被正确填充了。如何使用此DTO?你能详细说明一下吗?

bvjxkvbb

bvjxkvbb2#

在你的JSON中,在“规则”级别有一个字段“t”-"t": "TestRule",。在你的类Rule中,你没有属性“t”,所以,当我试图解析你的JSON时,我得到了一个错误,抱怨字段“t”未知。因此,我将@JsonIgnoreProperties(ignoreUnknown = true)添加到您的Rule类中,然后一切正常,所有字段都得到填充。下面是我的代码:

try {
        PolicyDto policyDto = JsonUtils.readObjectFromJsonString(jsonStr, PolicyDto.class);
    } catch (IOException ioe) {
        ...
    }

请注意,JsonUtils类是ObjectMapper类的一个瘦 Package 器。JsonUtils类附带由我编写和维护的MgntUtils开源Java库。这里是the Javadoc for JsonUtils class。MgntUtils库可以从Maven CentralGithub获得(包括源代码和Javadoc)

相关问题