Home > database >  Differences between lists of items and list of items
Differences between lists of items and list of items

Time:05-10

I was thinking about API path like .../lists/items?listId=1,2,3 to get a response like below.

// Map<Integer, List>
{
  1: [
    {
      "field": "item1"
    },
    {
      "field": "item11"
    }
  ],
  2: [
    {
      "field": "item2"
    },
    {
      "field": "item22"
    }
  ],
  3: [
    {
      "field": "item3"
    },
    {
      "field": "item33"
    }
  ]
}

or

// List<Object>
{
  [
    {
      "field": "item1"
    },
    {
      "field": "item11"
    },
    {
      "field": "item2"
    },
    {
      "field": "item22"
    },
    {
      "field": "item3"
    }
    {
      "field": "item33"
    },

  ]
}

But at the same time, I thought that the api path could be used to express to get following payload.

// List<List<Object>>
{
  [
    {
      "field": "item1"
    },
    {
      "field": "item11"
    }
  ], 
  [
    {
      "field": "item2"
    },
    {
      "field": "item22"
    }
  ],
  [
    {
      "field": "item3"
    },
    {
      "field": "item33"
    }
  ]
}

Is there correct answer for three cases above to write REST API path? If not, it would be appreciate to share your experience. Thanks.

CodePudding user response:

I'd use the first method.

  • The second is the worst, since you have to divide again the lists clientside, which is useless. JSON can send them separately
  • The third could be used, but you would have to recreate the connection id/list, since lists don't have a key.

The map method maintains the connection id/list and separate all the lists

  • Related