创建POJO并返回多个对象值

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

Creating POJO and returning multiple object values

问题

我正在从以下格式的JSON中读取:

  1. {
  2. "student" : [
  3. {
  4. "roll" : 1,
  5. "name" : "abc",
  6. "subjects" : [
  7. {
  8. "major" : "chemistry",
  9. "minor" : "maths"
  10. },
  11. {
  12. "major" : "biology",
  13. "minor" : "physics"
  14. }
  15. ]
  16. },
  17. {
  18. "roll" : 2,
  19. "name" : "xyz",
  20. "subjects" : [
  21. {
  22. "major" : "english",
  23. "minor" : "biology"
  24. },
  25. {
  26. "major" : "english",
  27. "minor" : "physics"
  28. }
  29. ]
  30. }
  31. ]
  32. }

我将 "student" 存储为一个字符串,并将每个学生的详细信息 - 'roll, name, subjects' 存储为一个POJO。

  1. 使用以下代码可以将 JSON 转换为对象:
  1. ObjectMapper mapper = new ObjectMapper();
  2. Map<String, List<POJO>> object = mapper.readValue(Json, new TypeReference<Map<String,List<POJO>>>(){});
  1. 要返回特定学生的所有主修和辅修课程,可以考虑使用一个Map。使用学生的名字作为键,将主修和辅修课程作为值存储在一个Map中,这样可以方便地进行查询。例如:
  1. Map<String, List<Map<String, String>>> studentCourses = new HashMap<>();

然后,将每个学生的主修和辅修课程列表存储在这个Map中。这样,当你想要返回特定学生的主修和辅修课程时,只需通过学生的名字在Map中查找对应的课程列表即可。

这是翻译好的内容,不包含其他额外内容。

英文:

I am reading from a JSON of the format :

  1. {
  2. &quot;student&quot; : [
  3. {
  4. &quot;roll&quot; : 1,
  5. &quot;name&quot; : &quot;abc&quot;,
  6. &quot;subjects&quot; : [
  7. {
  8. &quot;major&quot; : &quot;chemistry&quot;,
  9. &quot;minor&quot; : &quot;maths&quot;
  10. },
  11. {
  12. &quot;major&quot; : &quot;biology&quot;,
  13. &quot;minor&quot; : &quot;physics&quot;
  14. }
  15. ]
  16. },
  17. {
  18. &quot;roll&quot; : 2,
  19. &quot;name&quot; : &quot;xyz&quot;,
  20. &quot;subjects&quot; : [
  21. {
  22. &quot;major&quot; : &quot;english&quot;,
  23. &quot;minor&quot; : &quot;biology&quot;
  24. },
  25. {
  26. &quot;major&quot; : &quot;english&quot;,
  27. &quot;minor&quot; : &quot;physics&quot;
  28. }
  29. ]
  30. }
  31. ]
  32. }

I am storing "student" to a string and making each student details - 'roll, name, subjects' into a POJO.

  1. ObjectMapper mapper = new ObjectMapper(); <br />Map<String, List<POJO> object = mapper.readValue(Json, ?);
    <br />When I give 'Map.class', I get error and when I give new TypeReference<Map<String,List<RevisionedTenantQuery>>>(){}, it stores the package, not json to POJO. What do I give in place of '?'

  2. What is the best way to return all the major and minor of a particular student given the student name? Should I do a hashmap or list? Or is there any other way?

答案1

得分: 1

我假设你正在读取JSON字符串,并且需要将它们转换为POJO。在这种情况下,你需要按照以下方式设计你的POJO类:

  1. class Reports {
  2. private final List<Student> student;
  3. // getters & setters
  4. }
  5. class Student {
  6. private final int roll;
  7. private final String name;
  8. private final List<Subject> subjects;
  9. // getters & setters
  10. }
  11. class Subject {
  12. private final String major;
  13. private final String minor;
  14. // getters & setters
  15. }

然后你可以像下面这样使用:

  1. ObjectMapper mapper = new ObjectMapper();
  2. Reports reports = mapper.readValue(jsonString, Reports.class);
英文:

I am assuming that you are reading JSON string, and you need to convert them into POJO, in that case you need to design you POJO as below,

  1. class Reports{
  2. private final List&lt;Student&gt; student;
  3. // getters &amp; setters
  4. }
  5. class Student {
  6. private final int roll;
  7. private final String name;
  8. private final List&lt;Subject&gt; subjects;
  9. // getters &amp; setters
  10. }
  11. class Subject{
  12. private final String major;
  13. private final String minor;
  14. // getters &amp; setters
  15. }

Then you can use as below,

  1. ObjectMapper mapper = new ObjectMapper();
  2. Reports reports = mapper.readValue(jsonString, Reports.class);

POST Edited

huangapple
  • 本文由 发表于 2020年9月22日 07:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64000986.html
匿名

发表评论

匿名网友

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

确定