I am using Ruby on Rails 3 and I would like to handle properly web service responses finding a compromise between performance and utility\maneuverability.
I have to decide what type of response my web service must return back to the web client application. I am using JSON for trasmitting data and I would know if it is better to use hashes (for these I have to do extra work) or arrays.
For example I have this response:
[
{
"accounts" => {
[
"account" => {
"id" => 45,
"name" => "Test_name45",
"..." => ..."
}
"account" => {
"id" => 60,
"name" => "Test_name60",
"..." => ..."
}
]
}
},
{
"other" => {
"sub_other" => {...}
}
}
]
I would like to use something like (no array in “accounts”, from “account” to “id”)
{
"accounts" => {
"45" => {
"name" => "Test_name45",
"..." => ..."
}
"60" => {
"name" => "Test_name60",
"..." => ..."
}
},
{
"other" => {
"sub_other" => {...}
}
}
but the latter means more work for the web service…
How to make that? Any advice?
I would use whichever data structure makes sense. If you have a list of things then use an array where each element is a thing. I would probably simplify your data structure to something like this:
Now,
accountsis just an array of objects–no need to go looking things up by key. In Ruby this translates nicely: