我有以下数据,我想打印具有以下结构的项目列表。
让我知道我该怎么做。
我无法使用以下语法获取数据。
/* Data to Row Json from URL*/
NSString *MyRowJson = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
/* Copy data to Items from MyRowJson*/
NSDictionary *items = [MyRowJson objectFromJSONStringWithParseOptions:true];
/*get the deals data*/
NSMutableArray *ResponseData = [items objectForKey:@"deals"];
/*Get the count and based on this loop through the objects.*/
NSLog(@"deals data count is %d",[ResponseData count]);
这里我打印计数时遇到异常。
以下是我的数据结构。
{
"meta": {
"code": 200
},
"response": {
"deals": [
{
"id": 32373,
"date_added": "2011-01-13 12:12:50",
"end_date": "2011-01-14 10:00:00",
"active": 1,
"discount": {
"raw": 71,
"formatted": "71%"
},
"price": {
"raw": "85.00",
"formatted": "$85"
},
"value": {
"raw": "300.00",
"formatted": "$300"
},
"title": "$85 For $300 Babyface Facial At Park Avenue MedSpa",
"yahoo_title": "71% off Babyface Facial",
"url": "http://yahoo.com/aff/click/?deal=AvwTADtE&key=F374EFbM",
"yahoo_url": "http://yahoo.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/",
"mobile_url": "http://m.yahoo.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/",
"images": {
"image_big": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_display_image.jpg",
"image_small": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_small_image.jpg"
},
"division": {
"slug": "new-york",
"name": "New York",
"active": 1,
"time_zone_diff": -4,
"lat": "40.7142690000000000",
"lon": "-74.0059730000000000",
"url": "http://yahoo.com/new-york/"
},
"tags": [
{
"name": "Facial",
"slug": "facial",
"url": "http://yahoo.com/new-york/deals/facial/"
},
{
"name": "Spa",
"slug": "spa",
"url": "http://yahoo.com/new-york/deals/spa/"
}
],
"business": {
"name": "Park Avenue MedSpa",
"url": "",
"locations": [
{
"address": "565 Park Ave",
"locality": "New York",
"phone": "212-593-8821",
"lat": null,
"lon": null,
"state": NY,
"zip_code": "11211"
}
]
},
"source": {
"name": "LivingSocial",
"slug": "livingsocial",
"paid": 0,
"url": "http://yahoo.com/new-york/livingsocial"
}
}
]
}
}
答案 0 :(得分:3)
您正在正确地传递数据并从最顶层节点获取交易,但它位于响应节点下:
NSString *jsonData = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
NSError *error = nil;
NSDictionary *items = [jsonData JSONObjectWithData:data options:0 error:&error];
if (!items) {
NSLog(@"Could not parse json:%@", error);
return;
}
/*get the deals data*/
NSDictionary *response = [items objectForKey:@"response"];
NSArray *dealsArray = [response objectForKey:@"deals"];
/*Get the count and based on this loop through the objects.*/
NSLog(@"deals data count is %d",[dealsArray count]);
从JSON返回的对象也是不可变的,因此您不能将它们分配给可变对象。
您将需要使用不可变类型的copyMutable
方法来获取可变类型。
请记住,copyMutable
需要在非ARC项目中发布。