有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java类来生成给定的JSON字符串

我很难想出一个代表以下JSON的类。“家庭关系”是一组数组。每个数组都有person1和person2标识符以及person1和person2之间的关系。正如您可能已经猜到的,数组中值的顺序非常重要。例如,如果“12345”和“31142”位置切换,则表示“31142”是“12345”的父项,这是完全错误的

{
    "familyRelationships": [
        [
            "12345", 
            "SPOUSE", 
            "67890", 
            {
                "careTaker": false,
                "liveTogether": true
            }
        ],
        [
            "12345", 
            "PARENT", 
            "31142", 
            {
                "careTaker": true,
                "liveTogether": true
            }
        ],
        [
            "67890", 
            "PARENT", 
            "31142", 
            {
                "careTaker": true,
                "liveTogether": true
            }
        ]
    ]
}

共 (3) 个答案

  1. # 1 楼答案

    网上有一些工具可以帮你做到这一点

    package com.example;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "familyRelationships"
    })
    public class Example {
    
    @JsonProperty("familyRelationships")
    private List<List<String>> familyRelationships = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    @JsonProperty("familyRelationships")
    public List<List<String>> getFamilyRelationships() {
    return familyRelationships;
    }
    
    @JsonProperty("familyRelationships")
    public void setFamilyRelationships(List<List<String>> familyRelationships) {
    this.familyRelationships = familyRelationships;
    }
    
    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    

    这是用http://www.jsonschema2pojo.org/生成的仅有1个可能性

  2. # 2 楼答案

    JSON不会很好地成为POJO,因为数组中包含的类型不一致:

    例如

    [
      "12345", 
      "SPOUSE", 
      "67890", 
      {
        "careTaker": false,
        "liveTogether": true
      }
    ]
    

    String,String,String,Object。唯一有效的方法是使用Object[]List<Object>。所以familyRelationships实际上应该是一个List<List<Object>>。最终的结果将需要一系列的强制转换(可能还需要一些检查以确保给定索引中的项是您期望的类),但它会起作用

  3. # 3 楼答案

    这应该可以通过使用自定义反序列化器来实现

    在我看来,一个关系应该被建模为一个具有专有名称的java类。请注意,构造函数将^{}作为参数,并且我已经留下了我们的任何getter和setter:

    public class Relationship {
    
        private final String id1;
        private final String id2;
        private final Relation relation;
        private final boolean careTaker;
        private final boolean liveTogether;
    
        public Relationship(JsonNode base) {
            this.id1 = base.get(0).asText();
            this.id2 = base.get(2).asText();
            this.relation = Relation.valueOf(base.get(1).asText());
            this.careTaker = base.get(3).get("careTaker").asBoolean();
            this.liveTogether = base.get(3).get("liveTogether").asBoolean();
        }
    
        public enum Relation {
            PARENT,
            SPOUSE;
        }
    
    }
    

    我们还需要一个存储集合的类。这是您将顶级对象反序列化为的对象(再次省略getter和setter):

    @JsonDeserialize( using = FamillyRelationshipsDeserializer.class )
    public class FamillyRelationships {
    
        public List<Relationship> familyRelationships = new ArrayList<>();
    
    } 
    

    最后,我们需要实现上述类中引用的实际^{}。它应该如下所示。我使用this question作为参考:

    class FamillyRelationshipsDeserializer extends JsonDeserializer<FamillyRelationships> {
        @Override
        public FamillyRelationships deserialize(JsonParser jp, DeserializationContext ctxt) throws 
                IOException, JsonProcessingException {
            FamillyRelationships relationships = new FamillyRelationships();
            JsonNode node = jp.readValueAsTree();
            JsonNode rels = node.get("familyRelationships");
    
            for (int i = 0; i < rels.size(); i++) {
                relationships.familyRelationships.add(new Relationship(rels.get(i));
            }
    
            return relationships;
        }
    }
    

    我希望这会有所帮助,我还没有实际测试过任何一个,它甚至可能不会编译,但原则应该是正确的。我还假设JSON是您提供的格式。如果这不是保证,那么您需要进行适当的检查并处理任何偏差

    如果您还希望能够序列化所有内容,则需要实现^{}请参见this question以了解更多详细信息