简体   繁体   English

如何在 Spring 引导中格式化 JSON 列表响应

[英]How to format JSON list response in Spring Boot

I am creating an API as a personal project for a game I really enjoy.我正在创建一个 API 作为我真正喜欢的游戏的个人项目。 I am trying to figure out how to encapsulate each member of the JSON list with a particular value from each member.我想弄清楚如何用每个成员的特定值封装 JSON 列表的每个成员。

For example, my response looks like this:例如,我的回复如下所示:

[
    [
        {
            "createdAt": "2021-08-21T05:04:45.000+00:00",
            "updatedAt": "2021-08-21T05:04:45.000+00:00",
            "id": 1,
            "cardName": "Demon Fiend",
            ...
        },
        {
            "createdAt": "2021-08-21T05:04:58.000+00:00",
            "updatedAt": "2021-08-21T05:04:58.000+00:00",
            "id": 2,
            "cardName": "Stack",
            ...
        },
        {
            "createdAt": "2021-08-21T05:05:00.000+00:00",
            "updatedAt": "2021-08-21T05:05:00.000+00:00",
            "id": 3,
            "cardName": "Overflow",
            ...
        }
    ]
]

And, the ideal response would index them by either their cardName or id, like so:而且,理想的响应将通过他们的 cardName 或 id 索引他们,如下所示:

[
    [
        "Demon Fiend" : {
            "createdAt": "2021-08-21T05:04:45.000+00:00",
            "updatedAt": "2021-08-21T05:04:45.000+00:00",
            "id": 1,
            "cardName": "Demon Fiend",
            ...
        },
        "Stack" : {
            "createdAt": "2021-08-21T05:04:58.000+00:00",
            "updatedAt": "2021-08-21T05:04:58.000+00:00",
            "id": 2,
            "cardName": "Stack",
            ...
        },
        "Overflow" : {
            "createdAt": "2021-08-21T05:05:00.000+00:00",
            "updatedAt": "2021-08-21T05:05:00.000+00:00",
            "id": 3,
            "cardName": "Overflow",
            ...
        }
    ]
]

I think this would make it a lot easier for users to iterate through the data.我认为这将使用户更容易遍历数据。 Again, the "Demon Fiend" could just be 1: {... }同样,“恶魔恶魔”可能只是1: {... }

The JSON response you are getting is invalid - I recommend validate JSON using - [JSON Formatter]您收到的 JSON 响应无效 - 我建议使用 - [JSON Formatter]验证 JSON

Here is the code for your solution, Change siteInfo[i].cardName with whatever value you want to use eg cardName, id... (i used cardName in below code).这是您的解决方案的代码,将siteInfo[i].cardName更改为您想要使用的任何值,例如 cardName、id...(我在下面的代码中使用了 cardName)。

Enjoy: :)享受: :)

 var siteInfo = [ { "createdAt": "2021-08-21T05:04:45.000+00:00", "updatedAt": "2021-08-21T05:04:45.000+00:00", "id": 1, "cardName": "Demon Fiend" }, { "createdAt": "2021-08-21T05:04:58.000+00:00", "updatedAt": "2021-08-21T05:04:58.000+00:00", "id": 2, "cardName": "Stack" }, { "createdAt": "2021-08-21T05:05:00.000+00:00", "updatedAt": "2021-08-21T05:05:00.000+00:00", "id": 3, "cardName": "Overflow" } ] var map = new Map(); for(var i=0; i<siteInfo.length; i++) { map.set(siteInfo[i].cardName, siteInfo[i]); } let jsonObject = {}; map.forEach((value, key) => { jsonObject[key] = value }); console.log(JSON.stringify(jsonObject));

I must confess your question is bit confusing to me.我必须承认你的问题让我有点困惑。

I am assuming that you are returning a list of objects in your controller as the response.我假设您返回 controller 中的对象列表作为响应。 Like this,像这样,

public List<YourDtoClass> ...

If that's the case what you can do is return a Map instead.如果是这种情况,您可以做的是返回Map Like this,像这样,

public Map<String, YourDtoClass>

If you already created a List, you can crate Map as follows,如果您已经创建了一个列表,则可以按如下方式创建 Map,

return yourList.stream().collect(Collectors.toMap(YourDtoClass::getCardName, Function.identity()));

let this is your Class named SampleClass让这是你的 Class 命名为 SampleClass

   

     class SampleClass {
            "createdAt": "2021-08-21T05:04:45.000+00:00",
            "updatedAt": "2021-08-21T05:04:45.000+00:00",
            "id": 1,
            "cardName": "Demon Fiend",
            ...
      }
 

instead of using array of objects, use a map with key as "cardName" and value as SampleClass.不使用对象数组,而是使用 map,键为“cardName”,值为 SampleClass。 then you will get the desired form.然后你会得到想要的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM