使用xcode解析对象内的json数组

时间:2015-03-14 01:59:00

标签: ios json afnetworking

我目前正在开发一个简单的JSON项目,我无法让我的生活到达JSON文件的一部分来获取我需要的图像。

以下是我的JSON数据:

[
{
"id": 22591183,
"name": "HEART TSURIKAWA",
"permalink": "heart-tsurikawa",
"position": 1,
"price": 39.0,
"default_price": 39.0,
"tax": 0.0,
"url": "/product/heart-tsurikawa",
"status": "sold-out",
"on_sale": false,
"created_at": "2014-11-13T03:24:52.000Z",
"description": "★ From Japan!\r\n\r\n★ New condition\r\n\r\n★ A style from the bosozoku culture where handles (tsurikawa rings) were stolen from subways, trains, and buses and hung from tow hooks and in interiors as a sign of rebellion.",
"options": [
  {
    "id": 76352830,
    "name": "Default",
    "price": 39.0,
    "sold_out": true,
    "has_custom_price": false
  }
],
"shipping": [
  {
    "amount_alone": 6.0,
    "amount_with_others": 5.0,
    "country": {
      "id": 43,
      "name": "United States",
      "code": "US"
    }
  },
  {
    "amount_alone": 30.0,
    "amount_with_others": 20.0
  }
],
"images": [
  {
    "url": "http://images.bigcartel.com/bigcartel/product_images/153658111/-/photo-1.JPG",
    "secure_url": "https://images.bigcartel.com/bigcartel/product_images/153658111/-/photo-1.JPG",
    "width": 1500,
    "height": 1125
  },
  {
    "url": "http://images.bigcartel.com/bigcartel/product_images/153658114/-/photo-1-2.JPG",
    "secure_url": "https://images.bigcartel.com/bigcartel/product_images/153658114/-/photo-1-2.JPG",
    "width": 1500,
    "height": 1125
  },
  {
    "url": "http://images.bigcartel.com/bigcartel/product_images/153658117/-/photo-2.JPG",
    "secure_url": "https://images.bigcartel.com/bigcartel/product_images/153658117/-/photo-2.JPG",
    "width": 1500,
    "height": 1125
  }
],
"artists": [

],
"categories": [
  {
    "id": 8732692,
    "name": "Other",
    "permalink": "other",
    "url": "/category/other"
  }
]
},

它继续增加,但我无法达到的部分是图像/图像网址,以便我可以在TableView上显示图像。

我目前使用产品名称获取数组的代码(注意:我正在使用AFNetworking):

-(void)makeProductRequests
{
NSURL *url = [NSURL URLWithString:@"http://api.bigcartel.com/constantcollection/products.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    self.productArray = [NSMutableArray arrayWithArray:responseObject];

    NSLog(@"The Array: %@",self.productArray);

    [self.tableView reloadData];
    [self.refreshControl endRefreshing];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Request Failed: %@, %@", error, error.userInfo);

}];

[operation start];
}

1 个答案:

答案 0 :(得分:0)

您需要深入了解JSON以获取所需的数据。它使用NSDictionaryNSArray对象嵌套。

以下是一些代码,展示了如何从第一个产品中获取第一张图片:

self.productArray = [NSMutableArray arrayWithArray:responseObject];

NSDictionary *firstProduct = self.productArray.firstObject;
NSString *identifier = product[@"identifier"];
NSArray *images = product[@"images"];
NSDictionary *firstImage = images.firstObject;
NSString *imageURL = firstImage[@"url"];