用于在PHP中检索JSON数据的代码

时间:2013-01-25 21:17:23

标签: php iphone json

我在Objective-C应用程序中有一个JSON字符串,我想将它发送到服务器上的PHP脚本。

我应该使用哪些PHP代码来接收和解析这些数据?

- (IBAction)send:(id)sender{

NSString *jsonString = [selectedPerson.jsonDictionary JSONRepresentation];


NSLog(@"ESE ES EL JSON %@", jsonString);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init    ];
NSString*post = [NSString stringWithFormat:@"&json=%@", jsonStri    ng];
NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding     allowLossyConversion:NO];

NSLog(@"ESTO ES DATA %@", postData);

[request setURL:[NSURL URLWithSt    ring:@"http://www.mydomine.com/recive.php"]];    
[request setHTTPMethod:@"POST"];    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-    type"];
[request setHTTPBody:postData];
}

2 个答案:

答案 0 :(得分:2)

如果你看一下:

NSString*post = [NSString stringWithFormat:@"&json=%@", jsonString];

您将看到JSON将包含在名为json的POST变量中。

<强> receive.php

$json = $_POST['json'];
$decoded = json_decode($json);

答案 1 :(得分:1)

If your are sending your JSON in POST method , It can be received in PHP with the below code

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>