【问题标题】:How to solve json dictionary?如何解决json字典?
【发布时间】:2016-10-08 11:27:49
【问题描述】:

如何解决这个我尝试了很多次但无法获取“business_details”数组的 JSON 数组。我只获取第一个“business_category_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"
    }
  ]
},

使用 Json 数据

 -(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 *results = [json objectForKey:@"status"];

 namearray= [results valueForKey:@"business_category_name"];

 emailarray = [results valueForKey:@"email"];

 [self.tableview reloadData];

 }

【问题讨论】:

    标签: objective-c arrays json xcode uitableview


    【解决方案1】:

    ViewController.m

    @interface ViewController ()
    {
         NSArray * yourArray;
    }
    
    
    NSLog(@"response data %@",json);
    NSArray *results = [json objectForKey:@"status"];    
    yourArray = [[[results objectAtIndex:0] valueForKey:@"business_details"] objectAtIndex:0];
    

    这将为您提供 0 索引(第一个对象)的“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"
        }
    ]
    

    如果你想在你的tableview 中使用它,那么你需要写indexPath.row 来代替索引,如下所示。

    yourArray = [[[results objectAtIndex:indexPath.row] valueForKey:@"business_details"] objectAtIndex:0];
    

    【讨论】:

    • 感谢回复,我使用的 tableview indexPath.row 与但结果数组未声明的标识符错误相同。如何使用请解释
    • 我使用了我的数组,但结果显示未声明的标识符错误。请帮帮我,我很累。
    • 如果不检查您的项目,我将无法帮助您!因为现在我也不明白为什么会出现这样的错误。
    • 如何发送我的项目请发送您的电子邮件ID。请不要忽略。谢谢
    • 感谢重播,非常感谢亲爱的先生。工作非常好。 :) 非常感谢
    【解决方案2】:

    如果完整的输出是json 引用的字典,您可以这样做:

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response 
                                                                options:NSJSONReadingMutableLeaves
                                                                  error:&error];
    NSArray *status = json[@"status"];
    NSDictionary *firstStatus = status[0];
    NSArray *businessDatails = firstStatus[@"business_details"]
    // Likely you want to continue
    NSDictionary *firstBusinessDetail = businessDetails[0];
    NSMutableString *name = firstBusinessDetail[@"name"]M
    

    或者把它们放在一起:

    NSMutableArray *businessDatails = json[@"status"][0][@"business_details"];
    

    【讨论】:

    • UITableViewCell indexing in a do not load这是什么意思?
    • 打印字符串上的简单标签,但非打印名称字符串中的 TableView Cell 标签。 这个呢?
    • 感谢回复,解决我的问题。感谢您给予您宝贵的时间。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多