How would I send an NSArray filled with objects ov

2019-04-15 02:44发布

问题:

I have a product/shopping list (consisting of product objects with a name, product_id etc) on the iPhone side. I wish to send this list to the server where I will compare the list on the server with the one from the iphone (to merge the changes made and send the merged list back to the iphone).

How would I send my array over a HTTP POST to the server? I know you probably can't just send the array, you would have to store it in either xml or json probably.. But still I'd like to have some possibilities before I start coding :)

Thanks in advance,

LeWiOn

Edit: Solved :) Thanks for the help guys!

Answer: http://andyj.be.s79833.gridserver.com/blog/?p=65

回答1:

Json seems to be your best option. Xml is heavy and more complex to parse. SBJson library makes an excellent work at serializing object graph. I already made this kind of process but never try sending my array to the server, instead, I compared hash of both server and local and downloaded the new version if needed.

Good luck.

Edit Using SBJSON Library, creating the json representation of an object is made like :

[myObject JSONRepresentation]

SBJSON Library can be found here

Edit : Json is by nature a representation of an array or a dictionnary. If your object is one of those, everything will run fine, if not, you have to implement the proxyForJson method in your object in order to serialize it. This method should returns a dictionary whose keys are properties names and value are properties values.