4
NSLog(@"%@", request.responseString);

これにより、の出力が得られます{"errors":{"email":["is already taken"]}}

"is already taken"メールとメッセージ文字列を文字列に保存して、アラートに表示したいと思います。これらの 2 つの項目に 2 つの文字列でアクセスするにはどうすればよいですか?

4

4 に答える 4

4

応答文字列は、サーバーからの生の出力です。この場合、JSON エンコードされます。AFNetworking JSON 固有のクラスの 1 つ (つまりAFJSONRequestOperation) を使用して応答を JSON オブジェクトとして取得するか、 を使用して自分で解析することができますNSJSONSerialization。を使用することをお勧めしAFJSONRequestOperationます。

于 2013-07-19T09:46:37.897 に答える
0

私は以下を使用しましたが、もう少し堅牢なようです:

[requestOperation setCompletionBlockWithSuccess:success failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        id response = error.userInfo;
    if (response && [response isKindOfClass:[NSDictionary class]]) {
        NSDictionary *responseDictionary = (NSDictionary *)response;

        // AFNetworking hides the actual error response under this key 
        if ([responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey]) {
            id suggestedRecovery = [responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey];

            if ([suggestedRecovery isKindOfClass:[NSString class]]) {
                // Try to json decode string
                id json = [NSJSONSerialization JSONObjectWithData:[suggestedRecovery dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
                if (json && [json isKindOfClass:[NSDictionary class]]) {
                    responseDictionary = json;
                }
            }
        }

        // .. extract error message out of responseDictionary
    }
    }];
于 2013-10-23T02:24:44.360 に答える
-1
    NSData *responseData = [[error userInfo] objectForKey:@"data"];
    if ([responseData length] > 0)
    {

     NSString *str = [[NSString alloc] initWithData:responseData                                                                                encoding:NSUTF8StringEncoding];
      NSLog(@"%@", str);
    }
于 2013-07-19T10:57:08.577 に答える