如何反序列化json对象并分配给iOS中的NSDictionary

时间:2022-02-12 13:45:36

This is my code for handling server response.

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

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

This is the message Server response to me, NSLog JSON displays in console.

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

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

My question is: how do I deserialize the JSON and store it into a local variable NSDictionary *jsonData?

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

Any suggestions? Please give me some code example, thanks!

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

1 个解决方案

#1


19  

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

If you have NSString response

如果您有NSString响应

NSError *e = nil
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];

#1


19  

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

If you have NSString response

如果您有NSString响应

NSError *e = nil
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];