【发布时间】:2014-09-10 16:39:45
【问题描述】:
我有一个可变数组 stories,我正在尝试向其中添加 JSON 响应的副本,但它会产生 O 个故事,而数组中没有任何内容。我究竟做错了什么? JSON 对象的格式正确,我可以单独显示每个组件,但它们不会添加到数组中。
NSString *str=@"http://www.foo.com/some.php";
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;
NSMutableDictionary *response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
NSLog(@"Your JSON Object: %@", response);
NSLog(@"Location: %@", response[@"location"]);
NSLog(@"Service ID: %@", response[@"serviceID"]);
NSLog(@"Problem: %@", response[@"problem"]);
[stories addObject:[response copy] ];
NSLog(@"Workorders: %d", [stories count]);
NSLog(@"WO Defined: %@",stories);
【问题讨论】:
-
您能否举一个使用网络浏览器访问页面时的 JSON 示例?
-
你能解释一下你是如何初始化你的可变数组的吗:
stories -
对不起,它在 NSMutableArray *stories 文件的顶部;
-
看起来故事没有初始化。因为 Objective C 允许您向 nil 对象发送消息,所以您在调用 [stories addObject:[response copy]] 时不会遇到任何问题。
标签: json xcode nsmutablearray nsmutabledictionary