如何的NSData转换成的NSDictionary-iphone,objective-c

2025-05-21 08:54:37
推荐回答(1个)
回答(1):

您好,很高兴为您解答。


1. 请试试下面的代码。 NSError* error;

 NSDictionary* json = [NSJSONSerialization 
   JSONObjectWithData:responseData
 options:kNilOptions 
 error:&error];
NSArray* latestLoans = [json objectForKey:@"loans"];
NSLog(@"loans: %@", latestLoans);


2. 检查此链接我相信,但这只会及以上的ios 5的工作。 确保iOS 5或以上的。 

3. 检查您输入的json,会不会是根既不是一本字典,也不是一个数组?说必须指定在这种情况下该选项。#import

int main(int argc, char *argv[]) {
 NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
 NSData *jsonData = [@"{ \"key1\": \"value1\" }" dataUsingEncoding:NSUTF8StringEncoding];
 NSDictionary *jsonObject=[NSJSONSerialization 
   JSONObjectWithData:jsonData 
      options:NSJSONReadingMutableLeaves 
      error:nil];
 NSLog(@"jsonObject is %@",jsonObject);
 [p release];
}
输出:2012-09-26 16:06:51.610 Untitled[19164:707] jsonObject is {
 key1 = value1;
}


如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】


希望我的回答对您有所帮助,望采纳!


                                                                                                                             ~ O(∩_∩)O~