2016-02-10 2 views
0

Я попытался получить массив изображений из этих данных json. См. Мои веб-данные.Как я буду разбирать данные массива из nsdictionary.?

{"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"}} 

я получаю все значение ключа из этих данных с помощью следующего кода.

-(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]; 
} 

}

я получаю имя изображения из этих данных, но не в состоянии добавить массив детали продукта с точки зрения uicollection. после ошибки я получаю и разбиваю приложение. "Причина: '- [__ длина NSCFArray]: непризнанные селектор направил к экземпляру 0x7b875590' "

+0

это неправильно * * [image setObject: [словарь objectForKey: @ "image"] forKey: @ "image"]; ** –

+0

no sir it но я думаю, что изображение - это массив, поэтому я не могу добавить его в uicollectionview. –

+0

Вы задаете массив под символом 'image' и хотите использовать его как строку, это ваша проблема. –

ответ

2

изменить это

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

в

enter image description here

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]; 
+0

ok sir i реализовать его сейчас –

+0

@sandeeptomar - проверить ответ на обновление –

+0

должен ли я добавить этот код в блок блокировки? –

Смежные вопросы