Moo33
Moo33

Reputation: 1283

How to get multiple arrays into iOS app through URL

I was wondering if there's a way to return multiple arrays from a single php file to an iOS app.

I have managed to do it with one array, but when I add another array I seem to be getting null: (using the same method here to keep it short but in reality I'm getting results from different tables)

PHP:

function getArray() 
{
    $query = "query here";

    if ($result = $this->db->query($query)) 
    {
        $resultArray = array();

        while($row = mysqli_fetch_assoc($result))
        {
          $resultArray[] = $row;
        }

        echo json_encode($resultArray);
        $result->free();
    }
}

getArray();
getArray();

When I open my php file in my browser I see something like this:

[{"id":"1","name":"John"},{"id":"2","name":"Susan"}]
[{"id":"1","name":"John"},{"id":"2","name":"Susan"}]

Xcode:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"urlhere"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSArray *resultsArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"%@",resultsArray);
}

However, resultsArray seems to be returning (null). If I echo the array once instead of twice it seems to work. Is there a way I can get both arrays into Xcode and then use NSJSONSerialization on them? Thanks!

Upvotes: 1

Views: 614

Answers (1)

Chris Suen
Chris Suen

Reputation: 312

You should json_encode the small array into JSON String, then put them into another array. After you done insert all the small array into the big array, you should json_encode it and return them to your xcode.

In your xcode, try to use the parser framework/plugin in github or google done by others, and parse the NSData your retrieve from your php. It should now become NSArray/NSDictionary, depends on how your parse your data.


This is some sample how to parse my array, you may need to parse them twice in some situation. Try to NSLog the result and check see what you get after done parsing.

p.s. this will parse the NSData into NSDictionary, try to modify yourself to return NSArray if you need it for your situation.

- (NSDictionary *)parseJsonDataToNSDictionary:(NSData *)POSTReply
{
NSDictionary * jsonArray = [[NSDictionary alloc] init];
// Check iOS version
NSString *version = [[UIDevice currentDevice] systemVersion];
BOOL isAtLeast5 = [version floatValue] >= 5.0;
if(isAtLeast5) {
    NSError *e = nil;
    jsonArray = [NSJSONSerialization JSONObjectWithData:POSTReply options:NSJSONReadingMutableContainers error:&e];
    if (!jsonArray) {
        NSLog(@"Error parsing JSON: %@", e); //return false;
    } else { /* sucess parsing */ }
} else {
    // support for device which not iOS 5
    NSString *json_string = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    jsonArray = [parser objectWithString:json_string];
}
return jsonArray;
}

Upvotes: 2

Related Questions