0

重複の可能性:
ASIFormDataRequest を使用して json を php に送信する

Objective-C と iOS の開発は初めてです。

iPhone から PHPに送信し、NSMutableArray再度取得して 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_dumpの代わりにPHPで行うことから始め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 に答える