【问题标题】:POJO Creation from JSON Array (BufferReader)从 JSON 数组创建 POJO (BufferReader)
【发布时间】:2021-01-22 21:39:06
【问题描述】:

我正在尝试解组以下 JSON

[{
    "myId": "12851cb3087f51b4fb392b1fea36eef9508",
    "secondaryId": "787CFD4A-6B1D-4415-AD56-D075B535B890",
    "my_key": "keyABCD",
    "email": ""
}, {
    "myId": "12851cb3087f51b4fb392b1fea36eef9508",
    "secondaryId": "BFACD2F0-F5EF-4F05-AA6B-00E18CA907EF",
    "my_key": "keyABCD",
    "email": ""
}, {
    "myId": "12851cb3087f51b4fb392b1fea36eef9508",
    "secondaryId": "567DE8C0-B5B5-4961-B97A-A2DD374AEED1",
    "my_key": "keyABCD",
    "email": ""
}, {
    "myId": "12851cb3087f51b4fb392b1fea36eef9508",
    "secondaryId": "78a52d90-be6c-4d80-b79d-0e256028ba01",
    "my_key": "keyABCD",
    "email": "test@email.com"
}, {
    "myId": "12851cb3087f51b4fb392b1fea36eef9508",
    "secondaryId": "aeb148e7-fc88-4a71-8baa-63b6528e463e",
    "my_key": "keyABCD",
    "email": ""
}]

并且已经有一个具有上述 json 的 bufferreader (myBufferedReader)。 POJO

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
@AllArgsConstructor
public class MyPOJO {
    @Getter @Setter private String myId;
    @Getter @Setter private String secondaryId;
    @Getter @Setter private String my_key;
    @Getter @Setter private String email;

}

关于使用下面的映射器 -

ObjectMapper mapper = new ObjectMapper();
List<MyPOJO> eventList = mapper.readValue(myBufferedReader.readLine(),mapper.getTypeFactory().constructCollectionType(List.class, MyPOJO.class));

出现错误。请帮忙。 - (不存在像默认构造一样的创建者):无法从对象值反序列化(没有基于委托或属性的创建者)

如果上述方法不正确,请建议从 bufferreader 读取并创建使用 json 映射的 pojo 类列表的最佳方法。

问候, 丹

【问题讨论】:

  • 我发布了一个使用库javax.json.*的答案...它不使用lombok。我可以看到 lombok 做了什么,但我不知道您打算使用多少 JSON。自动构建 Java POJO 似乎有点过头了,但我不是 JSON + Java 方面的专家。这个链接的答案(下面)讨论了使用 lombokJSON 转换为 POJO - 尽管我看到你的问题是你有一个 JsonArray 有几个要解析的 POJO 实例...Lombok Answer
  • 这里有一些答案声称您需要 LombokJackson JSON Parser - 这是一个不同的 JSON Parser 。我真的相信,如果您有许多 JSON String's 要解析,并且有许多不同类型的 Object's...@987654322,那么这种类型的东西只有有价值 @, Lombok + Jackson JsonArray
  • @NoArgConstructor 也添加到您的班级顶部

标签: java arrays json


【解决方案1】:

通过在 POJO 类的顶部添加 @NoArgConstructor 添加您的默认构造函数,即空构造函数。 然后只需将缓冲区读取器 JSON 字符串转换为 POJO 列表,如下所示:

List<MyPOJO> eventList = mapper.readValue(myBufferedReader, new TypeReference<List<MyPOJO>>(){});

【讨论】:

    【解决方案2】:

    好吧,我不是万事通Java JSON。我有一个工具,每当我需要解析 JSON 时都会使用它。请注意,有多种工具可以解析 JSON String's,但我只会针对我使用的版本发布解决方案。我个人不会涉足 Java 的组件 Annotations,因为它们增加了如此巨大的复杂性,并且不会增加代码的任何价值。我不是来证明我的观点的,但我不会进入 Java Beans

    有一个名为 GSON 的库(据说)可以将 JSON String's 直接映射到 Java Object'sPOJO's正如你所说的那样)。不幸的是,我不熟悉 GSON 工具,它实际上可能能够按照您的要求使用 Annotations “自动”构建class MyPOJO

    这是我的解决方案,它仅使用标准 JSON 解析库,我在下面将其称为 javax.json.*。您必须使用 Google 搜索 来检索 JSON JAR

    import java.io.*;
    import javax.json.*;
    
    public class S
    {
        // I am just going to create the class for parsing this
        // If there is a GSON way to do this "Automatically", then you
        // should not use this Answer I have written to Stack Overflow
    
        public static class MyPOJO
        {
            public final String myId;
            public final String secondaryId;
            public final String myKey;
            public final String email;
    
            public MyPOJO(String myId, String secondaryId, String myKey, String email)
            { this.myId=myId; this.secondaryId=secondaryId; this.myKey=myKey; this.email=email; }
    
            public String toString()
            {
                return
                    "myId:         " + myId         + '\n' +
                    "seoondaryId:  " + secondaryId  + '\n' +
                    "myKey:        " + myKey        + '\n' +
                    "email:        " + email        + "\n\n";
            }
        }
    
        public static void main(String[] argv) throws IOException
        {
            // This reads the 'input.json' file into the Json parser using
            // a simple java.io.FileReader
            Reader r = new FileReader("input.json");
    
            // This reads the file, and retrieves the JsonArray that you
            // have provided in your original post.
            JsonArray ja = Json
                .createReader(r)
                .readArray();
            
            for (JsonObject jo : ja.getValuesAs(JsonObject.class))
            {
                String myId         = jo.getString("myId");
                String secondaryId  = jo.getString("secondaryId");
                String myKey        = jo.getString("my_key");
                String email        = jo.getString("email");
    
                // What *I* would do is to get rid of the Component Annotation, and simply 
                // use a Constructor.  I don't strongly believe in Java's Annotation Classes,
                // and I never use them.  If you can find an AUTOMATED WAY to do all of this,
                // YOU SHOULD ... - if you are willing to learn it all.
                // I HAVE NOT! :)
                
                // If there is an easy way to **DIRECTLY MAP** a JSON Object to a specified
                // class - and I believe that the GSON library is capable of directly mapping
                // JSON Object's to GSON Java POJO's (Java Object's), but I have not used them
                // before.  My own personal belief is that if it were easier, then learning the
                // GSON JAR Library and Java Documentation (JavaDoc) for GSON.
    
                // Here, though, a Constructor is what I would prefer myself.
                MyPOJO mp = new MyPOJO(myId, secondaryId, myKey, email);
                System.out.println(mp.toString());
            }
        }
    }
    

    上面的类输出到Shell Terminal的如下:

    @cloudshell:~$ java S
    myId:         12851cb3087f51b4fb392b1fea36eef9508
    seoondaryId:  787CFD4A-6B1D-4415-AD56-D075B535B890
    myKey:        keyABCD
    email:        
    
    
    myId:         12851cb3087f51b4fb392b1fea36eef9508
    seoondaryId:  BFACD2F0-F5EF-4F05-AA6B-00E18CA907EF
    myKey:        keyABCD
    email:        
    
    
    myId:         12851cb3087f51b4fb392b1fea36eef9508
    seoondaryId:  567DE8C0-B5B5-4961-B97A-A2DD374AEED1
    myKey:        keyABCD
    email:        
    
    
    myId:         12851cb3087f51b4fb392b1fea36eef9508
    seoondaryId:  78a52d90-be6c-4d80-b79d-0e256028ba01
    myKey:        keyABCD
    email:        test@email.com
    
    
    myId:         12851cb3087f51b4fb392b1fea36eef9508
    seoondaryId:  aeb148e7-fc88-4a71-8baa-63b6528e463e
    myKey:        keyABCD
    email:        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2020-10-23
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多