我正在接受的答案here但它不適用於我。我得到NULL
。Objective-C Json字符串到PHP數組
我產生JSON從陣列:
NSError* error;
NSData *result =[NSJSONSerialization dataWithJSONObject:self.fileNamesListForUpload options:0 error:&error];
NSString *displayJson = [[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding];
NSLog(@"json result %@",displayJson);
此打印["sample.pdf","sample-1.pdf"]
然後我使用下面的命令發佈字符串
捲曲-F 「名稱列表= [」 sample.pdf 「」 sample-1.pdf「]」url
在我的php代碼中;
//get json string
$jsonString = $_POST["nameList"];
var_dump($_POST["nameList"]);
$arrayOfNames=json_decode($jsonString,true);
var_dump($arrayOfNames);
echo "ArrayOfNames: ",$arrayOfNames,"\n";
結果是;
string(25) "[sample.pdf,sample-1.pdf]"
NULL
ArrayOfNames:
或者如果我加引號''我得到;
string(27) "'[sample.pdf,sample-1.pdf]'"
NULL
爲什麼「」在我使用_POST時被解僱? [sample.pdf,sample-1.pdf]
我發佈了["sample.pdf","sample-1.pdf"]
?
如何解析json字符串並將其放入數組?
實際上我使用編碼'編碼:NSUTF8StringEncoding'我更新的代碼,但問題是這樣的字符串'[ 「sample.pdf」,「樣品1 .pdf「]'通過_POST接收到這個'[sample.pdf,sample-1.pdf]'我沒有那部分。 – 2013-05-10 14:42:04
因爲你沒有做URL編碼。 'NSUTF8StringEncoding'聽起來不像是URL編碼的東西。URL編碼的參數應該如下所示:'%5B%22sample.pdf%22%2C%20%22sample.pdf%22%5D' – Halcyon 2013-05-10 15:30:53