1

I am creating an application that the user presses a button and every time they press the button, it adds a record in the array. I need to get this data into a external mysql database, the best method seems to be through php using JSON.

How do I convert my array into a JSON array? and what does the php code look like for inserting the array into the database?

1
  • 1
    I have been worked in several project connecting my iOS app with webservices using this example. divcode.blogspot.com/2012/08/… It's the more easy way to do this. Hope it help you Commented Apr 3, 2014 at 13:54

1 Answer 1

3

You can use SBJson library to convert

NSArray -> NSString

Import SBJson.h in your .m file and use following code

NSString *jsonString = [array JSONRepresentation];

You can implement php code using this tutorial.

Edit

As suggested by RobP, you can use NSJSONSerialization to convert NSArray to NSString

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArray 
                                               options:NSJSONWritingPrettyPrinted
                                                 error:&error];

if (!jsonData) 
{
    NSLog(@"Got an error: %@", error);
} 
else 
{
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
Sign up to request clarification or add additional context in comments.

1 Comment

SBJson can be a hassle depending on whether you use ARC or not, 64-it or not... since iOS5 there's the NSJSONSerialization class built in, when I moved to that it made our code smaller and more mainatainable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.