更新:私はサーバから返されたJSON形式をJSONlintでテストしましたが問題ありません。NSJSONSerialization + AFNetworkingでセレクタエラーが発生しました
JSONデータを返すPHPスクリプトのAFNetworking呼び出しで、NSJSONSerializationで例外が発生します。私は同じ問題で他の質問を見て、それらのソリューションを試しましたが、まだエラーが発生しています。
それは、このラインにクラッシュ:
NSError *e = nil;
NSMutableArray *jsonArray =
[NSJSONSerialization JSONObjectWithData: jsonData
options: NSJSONReadingMutableContainers
error: &e];
エラーログ:
2012-03-19 18:10:41.291 imageUploader [3538:207] *終端'NSCFArrray bytes:認識できないセレクタがインスタンスに送信されました。0x6867430'
私のJSONデータ、私はこのようになりますブラウザからPHPスクリプトを呼び出す:[{ "ユーザー"
: "binky"、 "パス": "binky-0a96f9aab5267c8.jpg"、」 "binky"、 "path": "binky-9cf844252c28553.jpg"、 "index": "102"}、{"user": "binky"、 "path" "binky-d6c749d25d33015.jpg"、 "インデックス": "103"}]
データのNSLogは、次のようになります
( { index = 101; path = "binky-0a96f9aab5267c8.jpg"; ユーザ= binky; }、 { index = 102; path = "binky-9cf844252c28553.jpg"; ユーザ= binky; }、 { index = 103; path = "binky-d6c749d25d33015.jpg"; ユーザ= binky; })
は最後に、私は有効なJSONデータを持っていることを確認するテストを実行します。
if ([NSJSONSerialization isValidJSONObject: jsonData]){
NSLog(@"Good JSON \n");
}
私のエラーの原因がどこにあるかだから私は理解することはできません。リトルヘルプ?
// AFNetworking操作+ブロック
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:myRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonData) {
NSLog(@"Success JSON data:\n %@ \n", jsonData); //log data
if ([NSJSONSerialization isValidJSONObject: jsonData]){
NSLog(@"Good JSON \n");
}
NSError *e = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error: %@", error);
[self.navigationController popToRootViewControllerAnimated:YES];
}];
ありがとうございました。入力されたjsondataを 'NSMutableArray'に変換することはできますが、ここで' NSJSONSerialization'が必要な理由は分かりません(これは私のようなサンプルコードの多くで使用されています)。 –