14

这是我处理服务器响应的代码。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
 NSLog(@"connectionDidFinishLoading : %@", [[NSString alloc] initWithData:self.data    encoding:NSUTF8StringEncoding]);
 }

这是消息服务器对我的响应,NSLog JSON 显示在控制台中。

connectionDidFinishLoading : {"ErrorCode":"CssParameterException","ErrorMessage":"An error has occurred, please try again later.","Success":false}

我的问题是:如何反序列化 JSON 并将其存储到局部变量中NSDictionary *jsonData

有什么建议么?请给我一些代码示例,谢谢!

4

1 回答 1

19
NSError *e = nil
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &e];

如果你有 NSString 响应

NSError *e = nil
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];
于 2012-08-30T10:56:57.613 回答