How to deserialize json object and assign to a NSDictionary in iOS

我的未来我决定 提交于 2019-12-05 12:46:57

问题


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.

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?

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


回答1:


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

If you have NSString response

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


来源:https://stackoverflow.com/questions/12195245/how-to-deserialize-json-object-and-assign-to-a-nsdictionary-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!