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

Posted

技术标签:

【中文标题】使用xcode解析对象内的json数组【英文标题】:Parsing json array within an object using xcode 【发布时间】:2015-03-14 01:58:59 【问题描述】:

我目前正在处理一个简单的 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"
  
]
,

它会继续,但我无法到达的部分是图像/图像 url,以便我可以在 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】:

您需要深入研究 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"];

【讨论】:

你只需使用一个循环来解析每一个。

以上是关于使用xcode解析对象内的json数组的主要内容,如果未能解决你的问题,请参考以下文章

iPhone JSON框架不解析不在对象或数组内的JSON字符串对象

Xcode 解析 JSON 返回 NULL

如何使用json方法解析android中没有键名的数组内的嵌套json编码数组?

将 json_encoded 数据解析为用于 UIPickerView 的数组 - Xcode

如何将 JSON 对象内的 JSON 数组传递给 jQuery 自动完成

带有属性+数组的Json对象,如何移动数组内的属性?