将 JSON 响应字段映射到 NSArray
Mapping JSON response field into NSArray
响应:
{"rsBody":
[{"productId":11,
"productImageUrl":"http:xxxx"},
{"productId":9,
"productImageUrl":"http:"xxxx"}]}
我知道这是一个重复的问题,但仍然在问,因为没有找到正确的方法。我从 php 服务器得到一些响应,在 array
中作为 JSON
包含两个对象。我想 map
两个对象的元素 productImageUrl
在 NSArray
中。结果数组应该有点像
NSArray =[{@"url":"productImageUrl1"},{@"url":@"ProductImageUrl2"}, nil];
productImageUrl1 = 第一个对象的元素,productImageUrl2 = 第二个对象的元素。
我正在解析响应并能够从 rsBody
中提取它。
NSDictionary* response=(NSDictionary*)[NSJSONSerialization
JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:@"rsBody"];
试试这个:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary* response = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:@"rsBody"];
for (NSDictionary *dict in rsBody)
{
NSMutableDictionary *dictURL = [[NSMutableDictionary alloc] init];
[dictURL setValue:[dict valueForKey:@"productImageUrl"] forKey:@"url"];
[arr addObject:dictURL];
}
NSLog(@"%@", arr);
响应:
{"rsBody":
[{"productId":11,
"productImageUrl":"http:xxxx"},
{"productId":9,
"productImageUrl":"http:"xxxx"}]}
我知道这是一个重复的问题,但仍然在问,因为没有找到正确的方法。我从 php 服务器得到一些响应,在 array
中作为 JSON
包含两个对象。我想 map
两个对象的元素 productImageUrl
在 NSArray
中。结果数组应该有点像
NSArray =[{@"url":"productImageUrl1"},{@"url":@"ProductImageUrl2"}, nil];
productImageUrl1 = 第一个对象的元素,productImageUrl2 = 第二个对象的元素。
我正在解析响应并能够从 rsBody
中提取它。
NSDictionary* response=(NSDictionary*)[NSJSONSerialization
JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:@"rsBody"];
试试这个:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary* response = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:@"rsBody"];
for (NSDictionary *dict in rsBody)
{
NSMutableDictionary *dictURL = [[NSMutableDictionary alloc] init];
[dictURL setValue:[dict valueForKey:@"productImageUrl"] forKey:@"url"];
[arr addObject:dictURL];
}
NSLog(@"%@", arr);