POJO从JSON数组(BufferReader)创建

huangapple go评论134阅读模式
英文:

POJO Creation from JSON Array (BufferReader)

问题

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.util.List;
  6. // ... (import statements and other code)
  7. // Your existing code for the MyPOJO class
  8. public class Main {
  9. public static void main(String[] args) {
  10. // Assuming you have your BufferedReader named myBufferedReader
  11. ObjectMapper mapper = new ObjectMapper();
  12. try {
  13. String jsonLine;
  14. StringBuilder jsonContent = new StringBuilder();
  15. while ((jsonLine = myBufferedReader.readLine()) != null) {
  16. jsonContent.append(jsonLine);
  17. }
  18. String json = jsonContent.toString();
  19. List<MyPOJO> eventList = mapper.readValue(json, new TypeReference<List<MyPOJO>>() {});
  20. // Now you have your list of MyPOJO objects
  21. for (MyPOJO pojo : eventList) {
  22. System.out.println(pojo.toString());
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }

Please note that I've provided the code translation for the Java code you provided. If you have any issues or further questions, feel free to ask.

英文:

I am trying to unmarshal the following JSON

  1. [{
  2. &quot;myId&quot;: &quot;12851cb3087f51b4fb392b1fea36eef9508&quot;,
  3. &quot;secondaryId&quot;: &quot;787CFD4A-6B1D-4415-AD56-D075B535B890&quot;,
  4. &quot;my_key&quot;: &quot;keyABCD&quot;,
  5. &quot;email&quot;: &quot;&quot;
  6. }, {
  7. &quot;myId&quot;: &quot;12851cb3087f51b4fb392b1fea36eef9508&quot;,
  8. &quot;secondaryId&quot;: &quot;BFACD2F0-F5EF-4F05-AA6B-00E18CA907EF&quot;,
  9. &quot;my_key&quot;: &quot;keyABCD&quot;,
  10. &quot;email&quot;: &quot;&quot;
  11. }, {
  12. &quot;myId&quot;: &quot;12851cb3087f51b4fb392b1fea36eef9508&quot;,
  13. &quot;secondaryId&quot;: &quot;567DE8C0-B5B5-4961-B97A-A2DD374AEED1&quot;,
  14. &quot;my_key&quot;: &quot;keyABCD&quot;,
  15. &quot;email&quot;: &quot;&quot;
  16. }, {
  17. &quot;myId&quot;: &quot;12851cb3087f51b4fb392b1fea36eef9508&quot;,
  18. &quot;secondaryId&quot;: &quot;78a52d90-be6c-4d80-b79d-0e256028ba01&quot;,
  19. &quot;my_key&quot;: &quot;keyABCD&quot;,
  20. &quot;email&quot;: &quot;test@email.com&quot;
  21. }, {
  22. &quot;myId&quot;: &quot;12851cb3087f51b4fb392b1fea36eef9508&quot;,
  23. &quot;secondaryId&quot;: &quot;aeb148e7-fc88-4a71-8baa-63b6528e463e&quot;,
  24. &quot;my_key&quot;: &quot;keyABCD&quot;,
  25. &quot;email&quot;: &quot;&quot;
  26. }]

and already have a bufferreader (myBufferedReader) which has the above json. POJO

  1. import lombok.AllArgsConstructor;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import lombok.ToString;
  5. @ToString
  6. @AllArgsConstructor
  7. public class MyPOJO {
  8. @Getter @Setter private String myId;
  9. @Getter @Setter private String secondaryId;
  10. @Getter @Setter private String my_key;
  11. @Getter @Setter private String email;
  12. }

On using below mapper -

  1. ObjectMapper mapper = new ObjectMapper();
  2. List&lt;MyPOJO&gt; eventList = mapper.readValue(myBufferedReader.readLine(),mapper.getTypeFactory().constructCollectionType(List.class, MyPOJO.class));

getting error. Please help. - (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

If above is not the correct way, please suggest the best way to read from bufferreader and create list of pojo class mapped with json.

Regards,
Dan

答案1

得分: 1

  1. Well, I am not a Master of all things **`Java JSON`**. I have a tool which I use whenever I need to parse **JSON**. Be aware that there are multiple tools for parsing **JSON `String's`**, but I am only going to post the solution for the version that I use. I, personally, do not get into Java's Component **`Annotations`** because they add such a tremendous amount of complexity, and do not add anything to value of the code. I am not here to prove my points, but I don't get into **Java Beans**.
  2. There is a library called the **GSON** Library that (supposedly) can map **JSON `String's`** directly to Java `Object's` (**POJO's** as you called them). I am, unfortunately, unfamiliar with the **GSON** Tool, and it might actually be able to "automatically" build a `class MyPOJO` using **`Annotations`** as you have requested.
  3. Here is my solution, which just uses the standard **JSON Parsing Library** which I call [`javax.json.*`](https://docs.oracle.com/javaee/7/api/javax/json/package-summary.html) below. You would have to retrieve the **JSON JAR** by looking for it using a **Google Search**.
  4. ```java
  5. import java.io.*;
  6. import javax.json.*;
  7. public class S
  8. {
  9. // I am just going to create the class for parsing this
  10. // If there is a GSON way to do this "Automatically", then you
  11. // should not use this Answer I have written to Stack Overflow
  12. public static class MyPOJO
  13. {
  14. public final String myId;
  15. public final String secondaryId;
  16. public final String myKey;
  17. public final String email;
  18. public MyPOJO(String myId, String secondaryId, String myKey, String email)
  19. { this.myId=myId; this.secondaryId=secondaryId; this.myKey=myKey; this.email=email; }
  20. public String toString()
  21. {
  22. return
  23. "myId: " + myId + '\n' +
  24. "seoondaryId: " + secondaryId + '\n' +
  25. "myKey: " + myKey + '\n' +
  26. "email: " + email + "\n\n";
  27. }
  28. }
  29. public static void main(String[] argv) throws IOException
  30. {
  31. // This reads the 'input.json' file into the Json parser using
  32. // a simple java.io.FileReader
  33. Reader r = new FileReader("input.json");
  34. // This reads the file, and retrieves the JsonArray that you
  35. // have provided in your original post.
  36. JsonArray ja = Json
  37. .createReader(r)
  38. .readArray();
  39. for (JsonObject jo : ja.getValuesAs(JsonObject.class))
  40. {
  41. String myId = jo.getString("myId");
  42. String secondaryId = jo.getString("secondaryId");
  43. String myKey = jo.getString("my_key");
  44. String email = jo.getString("email");
  45. // What *I* would do is to get rid of the Component Annotation, and simply
  46. // use a Constructor. I don't strongly believe in Java's Annotation Classes,
  47. // and I never use them. If you can find an AUTOMATED WAY to do all of this,
  48. // YOU SHOULD ... - if you are willing to learn it all.
  49. // I HAVE NOT! :)
  50. // If there is an easy way to **DIRECTLY MAP** a JSON Object to a specified
  51. // class - and I believe that the GSON library is capable of directly mapping
  52. // JSON Object's to GSON Java POJO's (Java Object's), but I have not used them
  53. // before. My own personal belief is that if it were easier, then learning the
  54. // GSON JAR Library and Java Documentation (JavaDoc) for GSON.
  55. // Here, though, a Constructor is what I would prefer myself.
  56. MyPOJO mp = new MyPOJO(myId, secondaryId, myKey, email);
  57. System.out.println(mp.toString());
  58. }
  59. }
  60. }

The following is output by the above class to the Shell Terminal:

  1. @cloudshell:~$ java S
  2. myId: 12851cb3087f51b4fb392b1fea36eef9508
  3. seoondaryId: 787CFD4A-6B1D-4415-AD56-D075B535B890
  4. myKey: keyABCD
  5. email:
  6. myId: 12851cb3087f51b4fb392b1fea36eef9508
  7. seoondaryId: BFACD2F0-F5EF-4F05-AA6B-00E18CA907EF
  8. myKey: keyABCD
  9. email:
  10. myId: 12851cb3087f51b4fb392b1fea36eef9508
  11. seoondaryId: 567DE8C0-B5B5-4961-B97A-A2DD374AEED1
  12. myKey: keyABCD
  13. email:
  14. myId: 12851cb3087f51b4fb392b1fea36eef9508
  15. seoondaryId: 78a52d90-be6c-4d80-b79d-0e256028ba01
  16. myKey: keyABCD
  17. email: test@email.com
  18. myId: 12851cb3087f51b4fb392b1fea36eef9508
  19. seoondaryId: aeb148e7-fc88-4a71-8baa-63b6528e463e
  20. myKey: keyABCD
  21. email:
英文:

Well, I am not a Master of all things Java JSON. I have a tool which I use whenever I need to parse JSON. Be aware that there are multiple tools for parsing JSON String&#39;s, but I am only going to post the solution for the version that I use. I, personally, do not get into Java's Component Annotations because they add such a tremendous amount of complexity, and do not add anything to value of the code. I am not here to prove my points, but I don't get into Java Beans.

There is a library called the GSON Library that (supposedly) can map JSON String&#39;s directly to Java Object&#39;s (POJO's as you called them). I am, unfortunately, unfamiliar with the GSON Tool, and it might actually be able to "automatically" build a class MyPOJO using Annotations as you have requested.

Here is my solution, which just uses the standard JSON Parsing Library which I call javax.json.* below. You would have to retrieve the JSON JAR by looking for it using a Google Search.

  1. import java.io.*;
  2. import javax.json.*;
  3. public class S
  4. {
  5. // I am just going to create the class for parsing this
  6. // If there is a GSON way to do this &quot;Automatically&quot;, then you
  7. // should not use this Answer I have written to Stack Overflow
  8. public static class MyPOJO
  9. {
  10. public final String myId;
  11. public final String secondaryId;
  12. public final String myKey;
  13. public final String email;
  14. public MyPOJO(String myId, String secondaryId, String myKey, String email)
  15. { this.myId=myId; this.secondaryId=secondaryId; this.myKey=myKey; this.email=email; }
  16. public String toString()
  17. {
  18. return
  19. &quot;myId: &quot; + myId + &#39;\n&#39; +
  20. &quot;seoondaryId: &quot; + secondaryId + &#39;\n&#39; +
  21. &quot;myKey: &quot; + myKey + &#39;\n&#39; +
  22. &quot;email: &quot; + email + &quot;\n\n&quot;;
  23. }
  24. }
  25. public static void main(String[] argv) throws IOException
  26. {
  27. // This reads the &#39;input.json&#39; file into the Json parser using
  28. // a simple java.io.FileReader
  29. Reader r = new FileReader(&quot;input.json&quot;);
  30. // This reads the file, and retrieves the JsonArray that you
  31. // have provided in your original post.
  32. JsonArray ja = Json
  33. .createReader(r)
  34. .readArray();
  35. for (JsonObject jo : ja.getValuesAs(JsonObject.class))
  36. {
  37. String myId = jo.getString(&quot;myId&quot;);
  38. String secondaryId = jo.getString(&quot;secondaryId&quot;);
  39. String myKey = jo.getString(&quot;my_key&quot;);
  40. String email = jo.getString(&quot;email&quot;);
  41. // What *I* would do is to get rid of the Component Annotation, and simply
  42. // use a Constructor. I don&#39;t strongly believe in Java&#39;s Annotation Classes,
  43. // and I never use them. If you can find an AUTOMATED WAY to do all of this,
  44. // YOU SHOULD ... - if you are willing to learn it all.
  45. // I HAVE NOT! :)
  46. // If there is an easy way to **DIRECTLY MAP** a JSON Object to a specified
  47. // class - and I believe that the GSON library is capable of directly mapping
  48. // JSON Object&#39;s to GSON Java POJO&#39;s (Java Object&#39;s), but I have not used them
  49. // before. My own personal belief is that if it were easier, then learning the
  50. // GSON JAR Library and Java Documentation (JavaDoc) for GSON.
  51. // Here, though, a Constructor is what I would prefer myself.
  52. MyPOJO mp = new MyPOJO(myId, secondaryId, myKey, email);
  53. System.out.println(mp.toString());
  54. }
  55. }
  56. }

The following is output by the above class to the Shell Terminal:

  1. @cloudshell:~$ java S
  2. myId: 12851cb3087f51b4fb392b1fea36eef9508
  3. seoondaryId: 787CFD4A-6B1D-4415-AD56-D075B535B890
  4. myKey: keyABCD
  5. email:
  6. myId: 12851cb3087f51b4fb392b1fea36eef9508
  7. seoondaryId: BFACD2F0-F5EF-4F05-AA6B-00E18CA907EF
  8. myKey: keyABCD
  9. email:
  10. myId: 12851cb3087f51b4fb392b1fea36eef9508
  11. seoondaryId: 567DE8C0-B5B5-4961-B97A-A2DD374AEED1
  12. myKey: keyABCD
  13. email:
  14. myId: 12851cb3087f51b4fb392b1fea36eef9508
  15. seoondaryId: 78a52d90-be6c-4d80-b79d-0e256028ba01
  16. myKey: keyABCD
  17. email: test@email.com
  18. myId: 12851cb3087f51b4fb392b1fea36eef9508
  19. seoondaryId: aeb148e7-fc88-4a71-8baa-63b6528e463e
  20. myKey: keyABCD
  21. email:

答案2

得分: 1

Add your default constructor, empty constructor, by adding @NoArgConstructor on top of your POJO class.
Then simply convert your buffer reader JSON string to a list of POJO like this:

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

Add your default constructor, empty constructor, by adding @NoArgConstructor top of your POJO class.
Then simply convert your buffer reader JSON string to a list of POJO like this:

  1. List&lt;MyPOJO&gt; eventList = mapper.readValue(myBufferedReader, new TypeReference&lt;List&lt;MyPOJO&gt;&gt;(){});

huangapple
  • 本文由 发表于 2020年10月8日 05:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定