parsing NSJSONReadingAllowFragments

前端 未结 2 644
北恋
北恋 2020-12-21 02:06

I am receiving some json data in my app:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingAllowFragments         


        
2条回答
  •  执念已碎
    2020-12-21 02:43

    It seems that your server sends "nested JSON": jsonResponse is a JSON string (not a dictionary). The value of that string is again JSON data representing a dictionary.

    In that case you have to de-serialize the JSON twice:

    NSString *jsonString = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingAllowFragments error:nil];
    NSData *innerJson = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:innerJson options:0 error:nil];
    
    NSString *email = jsonDict[@"email"];
    

提交回复
热议问题