【问题标题】:How to make nested json from single table如何从单个表中制作嵌套的 json
【发布时间】:2022-09-25 21:42:49
【问题描述】:

以下是查询后数据库表中的数据。查询在下面的存储库代码中提到。

+-----------+-----------+----------+
| Year      | Name      | Count    |
+-----------+-----------+----------+
| 2020      | Motor     | 12       |
| 2020      | Nut       | 35       |
| 2020      | Bolt      | 47       |
| 2020      | Engine    | 78       |
| 2020      | Oil       | 125      |
| 2020      | Filter    | 5        |
| 2020      | AC        | 10       |
| 2021      | Motor     | 22       |
| 2021      | Nut       | 76       |
| 2021      | Bolt      | 2        |
| 2021      | Engine    | 5        |
| 2021      | Oil       | 6        |
| 2021      | Filter    | 6        |
| 2021      | AC        | 12       |
+-----------+-----------+----------+

以下是我尝试过的代码:

代码:


    public ResponseEntity<Map<String, Object>> retrieveData() {
        List<DataDTO> stud = dataRepository.findAllNameCounts();

        Map<String, Object> parent = new HashMap<>();
        stud.forEach(d -> {
            String r = d.getYear();
            Map<String, String> child = new HashMap<>();
            child.put(\"name\", d.getName());
            child.put(\"count\", d.getCount());

            if (parent.containsKey(r)) {
                List<Map<String, String>> children = (List<Map<String, String>>) parent.get(r);
                children.add(child);
            } else {
                List<Map<String, String>> children = new ArrayList<>();
                children.add(child);
                parent.put(r, children);
            }
        });

        return ResponseEntity.ok().body(parent);
    }

数据DTO:

public interface DataDTO {

    public String getYear();

    public String getName();

    public String getCount();
}

存储库:

@Repository
public interface DataDTO extends JpaRepository<DataSummary, String> {

    @Query(name = \"SELECT YEAR(StartTime) as Year, Name, COUNT(*) AS COUNT FROM Data where  Name is not null group by Name, YEAR(StartTime)\", nativeQuery = true)
    List<DataDTO> findAllNameCounts();

}

数据总结:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = \"Data\")
public class DataSummary implements Serializable {

    private static final long serialVersionUID = 3269743987697633760L;

    @Id
    @Column(name = \"Id\", unique = true, nullable = false)
    public String id;

    @Column(name = \"StartTime\")
    public LocalDateTime StartTime;

    @Column(name = \"EndTime\")
    public LocalDateTime EndTime;

    @Column(name = \"Duration\")
    public String Duration;

    @Column(name = \"Name\")
    public String Name;

    @Column(name = \"Type\")
    public String Type;

}

JSON 目前我从上面的代码中得到。

{
\"2020\": [
{
\"name\": \"Motor\",
\"count\": \"12\"
},
{
\"name\": \"Nut\",
\"count\": \"35\"
},
{
\"name\": \"Bolt\",
\"count\": \"47\"
},
{
\"name\": \"Engine\",
\"count\": \"78\"
},
{
\"name\": \"Oil\",
\"count\": \"125\"
},
{
\"name\": \"Filter\",
\"count\": \"5\"
},
{
\"name\": \"AC\",
\"count\": \"10\"
}
],
\"2021\": [
{
\"name\": \"Motor\",
\"count\": \"22\"
},
{
\"name\": \"Nut\",
\"count\": \"76\"
},
{
\"name\": \"Bolt\",
\"count\": \"2\"
},
{
\"name\": \"Engine\",
\"count\": \"5\"
},
{
\"name\": \"Oil\",
\"count\": \"6\"
},
{
\"name\": \"Filter\",
\"count\": \"6\"
},
{
\"name\": \"AC\",
\"count\": \"12\"
}
]
}

预期的 JSON:

{
\"primeData\" :[
  {
    \"year\": 2021,
    \"data\": [
      {
        \"name\": \"Motor\",
        \"count\": 22
      },
      {
        \"name\": \"Nut\",
        \"count\": 76
      },
      {
        \"name\": \"Bolt\",
        \"count\": 2
      },
      {
        \"name\": \"Engine\",
        \"count\": 5
      },
      {
        \"name\": \"Oil\",
        \"count\": 6
      },
      {
        \"name\": \"Filter\",
        \"count\": 5
      },
      {
        \"name\": \"AC\",
        \"count\": 12
      }
    ]
  },
  {
    \"year\": 2020,
    \"data\": [
      {
        \"name\": \"Motor\",
        \"count\": 12
      },
      {
        \"name\": \"Nut\",
        \"count\": 35
      },
      {
        \"name\": \"Bolt\",
        \"count\": 47
      },
      {
        \"name\": \"Engine\",
        \"count\": 78
      },
      {
        \"name\": \"Oil\",
        \"count\": 125
      },
      {
        \"name\": \"Filter\",
        \"count\": 5
      },
      {
        \"name\": \"AC\",
        \"count\": 10
      }
    ]
  }
]
}

从上面的单个数据库表代码如何创建具有年份类型的嵌套 json 并从单个表中计数

  • 遍历表的每一行并为其创建一个 JSONObject。请向我们展示您的尝试,没有人会为您做作业(看起来像一个)。

标签: java spring-boot


【解决方案1】:

我想您将从 JPA 获取数据作为列表。然后您可以编写一个方法(或使用默认的杰克逊映射器)将该实体转换为单个 json 对象。

然后使用https://www.baeldung.com/java-org-json 中的JSONArray 将实体附加到此数组。

最后做一个 toString() 你会得到 JSON 结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多