问题
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