0

可能重复:
使用 ASIFormDataRequest 将 json 发送到 php

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

我想NSMutableArray从 iPhone 向 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,我的代码有什么问题???

4

2 回答 2

1

也许您应该在 print 之前使用 json_decode ?

于 2012-05-15T10:36:20.617 回答
0

从做一个开始var_dumpPHP 而不是print_r. 然后解码 JSON 对象

更新

[...]
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);

}

我认为这可能会奏效。咬在我的头顶上,但试一试。

于 2012-05-15T10:40:45.870 回答