简体   繁体   中英

How will i parse array data from nsdictionary.?

i tried to get image array from this json data. Please see my web data.

{"result":"Successful","data":{"id":"2","product_name":"6\" Round Plate","sku":"ECOW6RP","description":"Diameter 6.0 (inch) x Depth 0.6 (inch)\r\n\r\nPerfect for finger foods!","price":"42.89","business_price":"100.00","image":[{"image":"1454499251ecow6rp_01.jpg"}],"pack_size":"20","business_pack_size":"50","category":"2,3","tax_class":"1","created":"2016-01-19","altered":"2016-02-06 16:06:10","status":"1","deleted":"0","arrange":"1","delivery":"150.00"}}

i am getting every key value from this data by the following code.

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
        {
      [UIApplication sharedApplication].networkActivityIndicatorVisible  = NO;
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];

NSDictionary *arr = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"image"];
NSLog(@"arrr %@",arr);

 [productdetail removeAllObjects];




if (dictionary)
       {

    NSMutableDictionary *temp = [NSMutableDictionary new];
    NSMutableDictionary *image = [NSMutableDictionary new];
    [temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
    [temp setObject:[dictionary objectForKey:@"description"] forKey:@"description"];
    [temp setObject:[dictionary objectForKey:@"pack_size"] forKey:@"pack_size"];
    [temp setObject:[dictionary objectForKey:@"price"] forKey:@"price"];
    [image setObject:[dictionary objectForKey:@"image"] forKey:@"image"];

   // productdetail = [NSMutableArray arrayWithObjects:image,nil];
    [productimage addObject:temp];
    NSLog(@"productdetail %@", productdetail);


    NSIndexPath *indexhpath;
    _ProductName.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:@"product_name"];
    _ProductDesc.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:@"description"];
    _PackSize.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:@"pack_size"];
    price = [[productdetail objectAtIndex:indexhpath.row] objectForKey:@"price"];

    //Convert price into integer.
    int Price = [price intValue];
    NSInteger defaultpacks = 10;
    value = _TotalPrice.text = [NSString stringWithFormat:@"%d", Price*defaultpacks];
    NSLog(@"value %@", value);


     firstincrease = [price intValue];
     secondincrease = [value intValue];
    NSString *str = [NSString stringWithFormat:@"%d",firstincrease+secondincrease];
    number = [str intValue];

    firstdecrease =[price intValue];
    seconddecrease = [value intValue];
    NSString *str2 = [NSString stringWithFormat:@"%d",firstincrease+secondincrease];
    number2 = [str2 intValue];




}
if (productimage)
{
    [_imagecollectionview reloadData];
}

}

i am getting image name from this data but not able to add product detail array in uicollection view. following error i get and crash the application. " reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x7b875590' "

change this

 [image setObject:[dictionary objectForKey:@"image"] forKey:@"image"];

into

在此处输入图片说明

  NSMutableDictionary *temp = [NSMutableDictionary new];
NSMutableDictionary *image = [NSMutableDictionary new];
[temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
[temp setObject:[dictionary objectForKey:@"description"] forKey:@"description"];
[temp setObject:[dictionary objectForKey:@"pack_size"] forKey:@"pack_size"];
[temp setObject:[dictionary objectForKey:@"price"] forKey:@"price"];

 NSArray *ImageArray = [dictionary objectForKey:@"image"];
 for (NSDictionary *imageDict in ImageArray)
{
   [temp setObject:[imageDict objectForKey:@"image"] forKey:@"image"];
 }

  [productimage addObject:temp];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM