使用post和响应从NS到PHP获取NSMutableArray

时间:2012-05-15 10:29:48

标签: php objective-c ios ipad

  

可能重复:
  Send json to php using ASIFormDataRequest

我是Objective-c和iOS开发的新手。

我想从iPhone发送一个NSMutableArray到PHP,然后再次将其打印出来并使用NSLog打印它,以确保该数组已成功发送。

我使用了以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *phpUrl = @"http://dt-works.com/eman/bookMarks.php";

    NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
    NSMutableArray *arrayValue1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil]; 
    NSMutableArray *arrayValue2 = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", nil]; 

    NSDictionary *theReqDictionary1 = [NSDictionary dictionaryWithObjects:arrayValue1 forKeys:arrayKey];
    NSDictionary *theReqDictionary2 = [NSDictionary dictionaryWithObjects:arrayValue2 forKeys:arrayKey];

    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:theReqDictionary1,theReqDictionary2, nil];

    NSString *jsonString = [myArray JSONRepresentation];

    NSLog(@"%@", jsonString);


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];

     NSString *post = [[NSString alloc] initWithFormat:@"jsonString=%@&submit=", jsonString];
    //NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];


    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSArray *statuses = [parser objectWithString:json_string error:nil];
    int arraySize = [statuses count];

    NSMutableArray *number = [[NSMutableArray alloc] init];

    for (NSDictionary *status in statuses)
    {

        NSString *onee = [status objectForKey:@"one"];
        NSString *twoo = [status objectForKey:@"two"];
        NSString *threee = [status objectForKey:@"three"];


        [number addObject:status];

        NSLog(@"------------------ from browser-----------------------------------");
        NSLog(@"myOne %@ - myTwo: %@ - myThree: %@ ",onee ,twoo, threee);

    }

    NSLog(@"size of array is: %d", arraySize);
    NSLog(@"size of them is: %d", [number count]);
}

在PHP中,我编写了以下代码:

<?php
    header('Content-type:text/html;charset=utf-8');
    if (isset($_POST['submit'])) {
        $jsonString = $_POST['jsonString'];
        print_r($jsonString);
    }
?>

输出是:

[{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}]

size of array is: 0

size of them is: 0

并且数组的大小应该是2,我的代码有什么问题???

2 个答案:

答案 0 :(得分:1)

也许你应该在打印前使用json_decode?

答案 1 :(得分:0)

开始使用PHP而不是print_r执行var_dump。然后是decode the JSON object

更新

之后
[...]
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
[...]

添加以下内容:

SBJSON *json = [SBJSON new];
NSError *jsonError;
NSDictionary *parsedJSON = [json objectWithString:json_string error:&jsonError];

int arraySize = [parsedJSON count];

NSMutableArray *number = [[NSMutableArray alloc] init];

for (NSDictionary *status in parsedJSON)
{

    NSString *onee = [status objectForKey:@"one"];
    NSString *twoo = [status objectForKey:@"two"];
    NSString *threee = [status objectForKey:@"three"];


    [number addObject:status];

    NSLog(@"------------------ from browser-----------------------------------");
    NSLog(@"myOne %@ - myTwo: %@ - myThree: %@ ",onee ,twoo, threee);

}

我认为这可行。位于我的头顶,但试一试。