0

基本上,我有这个 if 语句:

NSString *loginResult = [resultsArray valueForKey:@"statusMsg"];
if ([loginResult isEqualToString:@"success"])
{
    [self successDialog]; // Success
}
else 
{
    [self failedDialog]; // Failed
}

如果成功,这是解析的 JSON:

{"results":{"statusMsg":"success"}}

如果失败,解析的 JSON 会有所不同:

{"results":{"statusMsg":"password incorrect."}}

我想将键statusMsg返回的字符串值传递给 failedDialog 方法内的警报视图。failedDialog 方法定义为:

- (void)failedDialog:(NSString *)errorMessage
{
    UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:kRegFailed
                                                     message:errorMessage
                                                    delegate:self
                                           cancelButtonTitle:kOK
                                           otherButtonTitles:nil, nil];
    dialog.tag = 2;
    [dialog show];  
}

我该怎么做才能让警报视图返回错误消息。蒂亚!

4

2 回答 2

1

如果您的 resultsArray 是 NSArray 对象,想知道为什么要这样做

NSString *loginResult = [resultsArray valueForKey:@"statusMsg"];

把它放在你的头文件中

- (void)failedDialog:(NSString *)errorMessage;

试试这个,假设你的jsonDict = { "results" :{ "statusMsg" : "password wrong." } }

[self failedDialog:[NSString stringWithString:[[jsonDict objectForKey:@"results"]objectForKey:@"statusMsg"]]];
于 2012-09-24T06:29:40.470 回答
1

编辑

NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSString *loginResult = [[jsonDictionary objectForKey:@"results"] objectForKey:@"statusMsg"];
if ([loginResult isEqualToString:@"success"])
{
    [self successDialog]; // Success
}
else 
{
    [self failedDialog:loginResult]; // Failed
}
于 2012-09-24T06:33:23.197 回答