ios - 如何提取JSON数组?

标签 ios objective-c json parsing nsarray

如何提取JSON数据字典。我尝试了很多次,但无法提取特定的值(value)。我只得到第一个“business_category_name”,但无法获取值“name”下的“business_details”。怎么可能请帮助。

谢谢

我的 JSON 数据

{
 "status": [
{
  "business_category_name": "Banks\/Credit Unions\/Landers",
  "business_details": [
    {
      "img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
      "name": "PNC Bank",
      "des": "PNC offers a wide range of services for all our customers, from individuals and small businesses, to corporations and government entities",
      "address": "",
      "email": "andrea.kendall@pnc.com",
      "phone": "260-422-5922",
      "member_since": "2016-05-31",
      "img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
      "website": "",
      "city": "Fort Wayne",
      "state": "IN",
      "zip": "46808"
    }
  ]
},
{
  "business_category_name": "Cleaning Services",
  "business_details": [
    {
      "img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
      "name": "tsst company",
      "des": "rudurgg ",
      "address": "2005 s calhoun ",
      "email": "",
      "phone": "2602496687",
      "member_since": "2016-05-31",
      "img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
      "website": "",
      "city": "fort wayne",
      "state": "in",
      "zip": "46825"
    }
  ]
}
 ]
 }

我的代码

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL  URLWithString:@"my url"]];
    response =[[NSMutableData alloc]init];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSError *error;
     NSLog(@"Error in receiving data %@",error);
     NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
     NSLog(@"response data %@",json);
     NSArray *items = [json objectForKey:@"status"];
     pass = [items valueForKey:@"business_category_name"];
     NSLog(@"get category %@",pass);
}

最佳答案

你可以这样做,

NSArray *items = [json objectForKey:@"status"];

NSDictionary *firstObjOfItems = [items objectAtIndex:0];

NSString *business_category_name = [firstObjOfItems valueForKey:@"business_category_name"];

NSArray *business_details_Array = [firstObjOfItems objectForKey:@"business_details"];

NSDictionary *first_Object_of_business_details = [business_details_Array objectAtIndex:0]
;

NSString *name = [first_Object_of_business_details valueForKey:@"name"];
NSLog(@"name : %@",name);

您可以类似地获取其他参数。您应该使用 for 循环遍历每个对象。这只是如何到达 json 数据中的末尾元素的示例。

希望这会有所帮助:)

关于ios - 如何提取JSON数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38217327/

相关文章:

objective-c - AVCaptureVideoPreviewLayer 不显示

ios - Swift 中的不可变/可变集合

ios - NSURLConnection 检索 JSON

javascript - 你如何在 JavaScript 中编写来自 JSON 的空键?

ios - iOS 后台运行任务

ios - 将文件上传到设备上的文件夹,文件共享 ios

ios - 以编程方式创建的 UISplitViewController 没有滑动手势来显示/隐藏 master

javascript - 如何在 html 表格中显示 JSON 数据

ios - JSON 错误 : NSInvalidArgumentException reason Invalid top-level type in JSON write in Swift3 iOS

ios - Swift - App Store Connect 版本无法检测 Catalyst 环境