tags:

views:

550

answers:

4

I have an iPhone app with an array of objects that I want to send to a PHP script and have them be stored in a mySQL database. The objects in the array contain only floating points and strings, nothing special.

From what I understand the best way to send the array to the php script is to convert the array into an JSON, send it to the php script via http post, and do a json_decode on the other end. However I'm having a hard time figuring out how to convert the array into a json object.

Can someone give me a pointer of where to start?

+4  A: 

http://code.google.com/p/touchcode/wiki/TouchJSON

"TouchJSON is parser and generator for JSON implemented in Objective C.

It is based on my CocoaJSON code: http://toxicsoftware.com/cocoajson/

Here is how to use it: TouchJSONHowTo"

Kenny Winker
A: 

JSON is a simple text format. You can write the string out yourself by looping over your array if you don't want to use a library.

http://www.json.org/example.html

Scott Saunders
+3  A: 

Start with the JSON library for objective-c:

http://code.google.com/p/json-framework

That will make the serialization much easier, as it has a method to convert an NSArray to JSON.

http://json-framework.googlecode.com/svn/trunk/documentation/interfaceSBJSON.html#830175bff0fbef8ccb82da852a154b48

From there you can post using different mechanisms, but NSURLConnection is the easiest. You can do synch or asynch, depending on your needs.

You will need to set some headers on the request for json:

NSMutableURLRequest * r = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[r addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[r setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];    
Andrew Kuklewicz
A: 

im looking for the inverse. I took the CS193P stanford course online and ran thru presence assignment and I have a related question:

Twitter API calls custom username.json files to get the json values for the app. I've made an app that fetches the info from a mysql db via a php page that $json->encode's the fetch object as a json value passed to the ios app using the same, but modified TwitterHelper class.

But so far I've only fetched generic data from the tables. Now I wanna get the data specific to a user by passing a username value from the ios app to the php. But I'm stumped on how to create the username.json type files?

mars