user1032531 user1032531 - 1 month ago 6
JSON Question

Sending pairs of values to the server

I need to send a bunch of pairs of values to a PHP server (and being "pairs", each of the two values are related), and will be using a GET request. Either of the following will work. What is the correct way to do this?

[{a:123,b:321}, {a:111,b:222}, {a:333,b:111}, {a:222,b:111}, ...]


or

[123,111,333,222] and [321,222,111,111]

Answer

There is not good or bad way. You just need to send data and both ways seem proper. Only advantange of second way is that it is compressed and pass lesser data through network while first one can help you to write lesser code as you can directly use json libraries.

If data usage in not a concern then you should go with first method as it will be more convenient to handle via code.

If the usage is from the perspective of some third person who uses the API, the it depends on how the data is related. If pair of a and b is related, then first way is preferred but if all a's are related together and b's are related separately the second way should be used.