0

私はiPhoneが初めてで、NSMutable配列をjson文字列に変換してから、リクエストを使用してこの文字列をphpファイルに送信し、NSLogへの応答を使用して再度印刷して、正常に送信されたことを確認しようとしています。だから私はviewDidLoadで次のコードを書きました

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);



 NSDictionary *questions = nil;
NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:jsonString forKey:@"jsonString"];
[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request2 error];
[request2 startSynchronous];

if (!error) { 
    NSData *response = [request responseData];
    NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    questions = [json objectFromJSONString];
    NSLog(@"Data: %@", questions);

} 

bookMarks.php のコードは次のとおりです。

$handle = fopen('php://input','r');
$jsonInput = $_POST['jsonString'];
print_r($jsonInput);

しかし、それは私に与えます:

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

データを次のようにしたい: [{"one":"1","two":"2","three":"3"},{"one":"11","two":"22 ","three":"33"}] これを行う方法??

4

1 に答える 1

0

これを試してみてください。JSON 文字列をキーと共に投稿する代わりに、それを Data としてスクリプトに投稿して読み込む必要があります。現在、スクリプトでは $handle を何にも使用していません...

NSURL *link = [NSURL URLWithString:[phpUrl     stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request appendPostData:[jsonString  dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setTimeOutSeconds:120];
[request setDelegate:self];

そしてあなたのphpコード..

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);
于 2012-05-27T14:49:06.780 に答える