将数组数据显示到一个字典中?

时间:2016-05-30 07:30:26

标签: ios objective-c json nsdictionary

我有这个解析数据

你好{"回应":" 200"," category-list":true," category":[{&#34 ; prod_id":" 1"," prod_name":"服装&配件"" prod_status":" 1"" prod_order":" 1"" prod_slug&#34 ;: "服装配件"},{" prod_id":" 2"," prod_name":"汽车服务", " prod_status":" 1"" prod_order":" 2"" prod_slug":"汽车-services"},{" prod_id":" 12"," prod_name":" Building&Construction"," prod_status":" 1"" prod_order":" 11"" prod_slug":"建筑和施工& #34;},{" prod_id":" 9"," prod_name":" Business&专业服务"," prod_status":" 1"," prod_order":" 8"," prod_slug" :" business-professional-services"},{" prod_id":" 10"," prod_name":" Business Services&# 34;," prod_status":" 1"" prod_order":" 9"" prod_slug":&# 34;商业服务"},{" prod_id":" 11"," prod_name":"社区&教育"" prod_status":" 1"" prod_order":" 10"" prod_slug&#34 ;: "社区教育"},{" PROD_ID":" 13"" PROD_NAME":"教育",& #34; prod_status":" 1"" prod_order":" 12"" prod_slug":"教育&# 34;},{" prod_id":" 14"," prod_name":"娱乐&媒体"" prod_status":" 1"" prod_order":" 13"" prod_slug&#34 ;: "娱乐媒体"},{" prod_id":" 15"," prod_name":"家庭& 34社区"" prod_status":" 1"" prod_order":" 14"" prod_slug&#34 ;: "家庭社区"},{" PROD_ID":" 16"" PROD_NAME":"金融",& #34; prod_status":" 1"" prod_order":" 15"" prod_slug":"金融&# 34;},{" prod_id":" 17"," prod_name":" Finance&法律"" prod_status":" 1"" prod_order":" 16"" prod_slug&#34 ;: " finance-legal"},{" prod_id":" 18"," prod_name":" Food&餐饮和#34;" prod_status":" 1"" prod_order":" 17"" prod_slug&#34 ;: "食品餐饮和#34;},{" PROD_ID":" 3"" PROD_NAME":"货",& #34; prod_status":" 1"" prod_order":" 3"" prod_slug":" bgdr&# 34;},{" prod_id":" 19"," prod_name":" Health&医药"" prod_status":" 1"" prod_order":" 18"" prod_slug&#34 ;: "健康医学"},{" prod_id":" 20"," prod_name":" Home&花园"" prod_status":" 1"" prod_order":" 19"" prod_slug&#34 ;: " home-garden"},{" prod_id":" 21"," prod_name":" Home Repair&改进"" prod_status":" 1"" prod_order":" 20"" prod_slug&#34 ;: "家庭维修改进"},{" prod_id":" 22"," prod_name":"工业用品&服务"" prod_status":" 1"" prod_order":" 21"" prod_slug&#34 ;: "工业用品 - 服务"},{" prod_id":" 23"," prod_name":"信息技术" ;," prod_status":" 1"" prod_order":" 22"" prod_slug":&#34 ;信息技术"},{" PROD_ID":" 24"" PROD_NAME":"法定"" prod_status":" 1"" prod_order":" 23"" prod_slug":"法律"} ,{" prod_id":" 7"," prod_name":"我的新类别"," prod_status":& #34; 1"" prod_order":" 6"" prod_slug":"新类别"},{&# 34; prod_id":" 8"," prod_name":"我的测试类别"," prod_status":" 1& #34;" prod_order":" 7"" prod_slug":"我的测试功能的类别"},{" PROD_ID":" 4"" PROD_NAME":"小球"" prod_status&#34 ;: #&34; 1"" prod_order":" 4"" prod_slug":" gdgggdsgdfgdg"},{&#34 ; prod_id":" 25"," prod_name":"个人护理"," prod_status":" 1&#34 ;," prod_order":" 24"" prod_slug":"个人护理"},{" PROD_ID" :" 26"," prod_name":"公用事业&环境"" prod_status":" 1"" prod_order":" 25"" prod_slug&#34 ;: "公共事业 - 环境"},{" prod_id":" 27"," prod_name":"房地产&#34 ;," prod_status":" 1"" prod_order":" 26"" prod_slug":&#34 ;房地产"},{" prod_id":" 28"," prod_name":"房地产&保险"" prod_status":" 1"" prod_order":" 27"" prod_slug&#34 ;: "地产,保险"},{" PROD_ID":" 29"" PROD_NAME":"购物" " prod_status":" 1"" prod_order":" 28"" prod_slug":" shopping"},{" prod_id":" 30"," prod_name":" Shopping&专卖店"," prod_status":" 1"," prod_order":" 29"," prod_slug" :"购物专卖店"},{" prod_id":" 31"," prod_name":"体育&娱乐"" prod_status":" 1"" prod_order":" 30"" prod_slug&#34 ;: "体育娱乐"},{" prod_id":" 6"," prod_name":"运输服务", " prod_status":" 1"" prod_order":" 5"" prod_slug":"输送及#34;},{" PROD_ID":" 32"" PROD_NAME":"运输"" prod_status" :" 1"" prod_order":" 31"" prod_slug":"运输"},{&# 34; PROD_ID":" 33"" PROD_NAME":"旅行"" prod_status":" 1&#34 ;," prod_order":" 32"" prod_slug":"行驶"},{" PROD_ID":& #34; 34"," prod_name":" Travel&倒伏"" prod_status":" 1"" prod_order":" 33"" prod_slug&#34 ;: "行驶倒伏"},{" PROD_ID":" 35"" PROD_NAME":"未归类",& #34; prod_status":" 1"" prod_order":" 34"" prod_slug":"未分类的&# 34;}]}

我试过这个: -

- (IBAction)SelectCategory:(id)sender
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:@"http://d.com/category"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"hello %@",str);

    NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

    categoryArray =[[NSArray alloc]init];

    CategoryDict = [[NSMutableDictionary alloc]init];
    categoryArray = [dict1 allValues];
    CategoryDict = [[categoryArray objectAtIndex:0]valueForKey:@"prod_name"];

    NSLog(@"category dict = %@",CategoryDict);
    [CategoryTable reloadData];
    CategoryTable.hidden = FALSE;


}

如何获得" prod_name"数据转换为cell的{​​{1}}? 谢谢提前。

2 个答案:

答案 0 :(得分:1)

喜欢

您的ViewDidLoad

 @interface XXXXViewController ()
{
 NSMutableArray *categoryArray;
}

 - (void)viewDidLoad
 {
[super viewDidLoad];
 categoryArray =  [[NSMutableArray alloc]init];
  }

  NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

  categoryArray  =  [dict1 valueForKey:@"category"] ;  

    if (categoryArray.count > 0)      
    {
     CategoryTable.hidden = FALSE;
    [CategoryTable reloadData];
    }
}

并访问LIK

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [categoryArray count];
}

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 cell.textLabel.text=[NSString stringWithFormat:@"%@ ",[[categoryArray objectAtIndex:indexPath.row] objectForKey:@"prod_name"]];
   }

答案 1 :(得分:0)

使用 id ,如下所示

这只是一个工作演示,供您理解

正如您在回答中所看到的那样,您将获得一系列词典。使用 id 告诉编译器您要访问哪个特定的键值对,例如:

categoryArray =[[NSArray alloc]init];
categoryArray = [dict1 allValues];
id keyValuePair = categoryArray[indexPath.row];

//分配给你的tableview单元格

cell.product_name_label.text = keyValuePair[@"prod_name"];