在文本字段中解析JSON文本(我的结果为NULL)

时间:2013-05-21 14:18:34

标签: ios json parsing textview

我正在制作一个iOS应用程序,它通过API与数据库通信。 API将有效的JSON发送到应用程序,但应用程序不会给出错误,但会产生另一个结果:NULL。

以下是iOS应用的代码:

 // Start hud
 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 hud.labelText = @"Zoeken...";

 return TRUE;
 }

    - (void)requestFinished:(ASIHTTPRequest *)request
   {    
 [MBProgressHUD hideHUDForView:self.view animated:YES];
if (request.responseStatusCode == 400) {
    textView.text = @"Invalid code";        
} else if (request.responseStatusCode == 403) {
    textView.text = @"Code already used";
} else if (request.responseStatusCode == 204) {
    textView.text = @"No content";
} else if (request.responseStatusCode == 412) {
    textView.text = @"Precondition Failed";
} else if (request.responseStatusCode == 200) {
    NSString *responseString = [request responseString];
    NSDictionary *responseDict = [responseString JSONValue];
    // NSString *naam = [responseDict objectForKey:@"DEB_NR"];

    NSString *part0 = [responseDict objectForKey:@"klntnr"];
    NSString *part1 = [responseDict objectForKey:@"klntnm"];
    NSString *part2 = [responseDict objectForKey:@"adrs"];
    NSString *show = [NSString stringWithFormat:@"\r%@\r%@\r%@" , part0 , part1 , part2 ];

   // if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {
        textView.text = [NSString stringWithFormat:@"Resultaten: %@", show];
  //  } else {
       // textView.text = [NSString stringWithFormat:@"Resultaat: %@", unlockCode];
  //  }

  } else {
      textView.text = @"Unexpected error API ERROR";
  }

}

 - (void)requestFailed:(ASIHTTPRequest *)request
 {    
 [MBProgressHUD hideHUDForView:self.view animated:YES];
 NSError *error = [request error];
  textView.text = error.localizedDescription;
  }

 @end

提前致谢,

莫里斯。

1 个答案:

答案 0 :(得分:-1)

如果您只显示JSON字符串,那将非常有用; 但试试这个

else if (request.responseStatusCode == 200) {

NSError* error;
NSString *responseString = [request responseString];
NSArray *responseObj = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &error];
NSString *part0=@"";
NSString *part1=@"";
NSString *part2=@"";

for (int i=0; i<[responseObj count]; i++) {
    NSDictionary *dataDict = [responseObj objectAtIndex:i];
    NSString *part0 = [dataDict objectForKey:@"klntnr"];
    NSString *part1 = [dataDict objectForKey:@"klntnm"];
    NSString *part2 = [dataDict objectForKey:@"adrs"];
}
NSString *show = [NSString stringWithFormat:@"\r%@\r%@\r%@" , part0 , part1 , part2 ];
textView.text = [NSString stringWithFormat:@"Resultaten: %@", show];
}