我试图从此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];
关于ios - 我将如何解析nsdictionary中的数组数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35308534/