如何反序列化JSON对象,并分配给iOS中放入NSDictionary(How to deseria

2019-07-31 06:56发布

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

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

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

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

我的问题是:我怎么反序列化JSON的,并将其存储到一个局部变量NSDictionary *jsonData

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

Answer 1:

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];


文章来源: How to deserialize json object and assign to a NSDictionary in iOS