[英]How will i parse array data from nsdictionary.?
我試圖從此json數據獲取圖像數組。 請查看我的網絡數據。
{"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'”
改變這個
[image setObject:[dictionary objectForKey:@"image"] forKey:@"image"];
進入
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.