Array of objects vs Object of Objects

后端 未结 6 1087
有刺的猬
有刺的猬 2020-12-13 05:56

The issue is to decided the trade offs between following notations:

JSON based:

\"users\": {
    \"id1\": {
        \"id\": \"id1\",         


        
6条回答
  •  离开以前
    2020-12-13 06:28

    On the server side, Arrays are stored as simple Lists: ArrayList, while Objects are either stored as maps: HashMap or, mostly, as Java Objects.

    In order to convert Java Entities to and from JSON, you can take a look at the Jackson project which does all that for you.

    I wouldn't worry about any performance differences between those two variants. It's more important to have an understandable, semantic API, so you should base your desicion on the business case rather than performance.

    Looking at your example, I think an Array is the better approach, since you want to return a list of users which are all equal. Sending the id twice makes no sense imho and increases the amount of data that has to be transmitted.

    Furthermore, since Arrays are much simpler to store and to iterate in Java, they should also provide better performance than Objects.

    Some general differences:

    • Arrays preserve the order
    • Arrays can contain duplicate entries
    • Objects often have a bigger storage/network overhead
    • Arrays are faster to iterate (on the server side)

提交回复
热议问题