简体   繁体   中英

How to format JSON list response in Spring Boot

I am creating an API as a personal project for a game I really enjoy. I am trying to figure out how to encapsulate each member of the JSON list with a particular value from each member.

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:

[
    [
        "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: {... }

The JSON response you are getting is invalid - I recommend validate JSON using - [JSON Formatter]

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).

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. Like this,

public List<YourDtoClass> ...

If that's the case what you can do is return a Map instead. Like this,

public Map<String, YourDtoClass>

If you already created a List, you can crate Map as follows,

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

let this is your Class named 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. then you will get the desired form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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