JSON 解析:从 NSDictionary 中检索数据并将其存储在数组中
JSON Parsing : Retrieving data from the NSDictionary and storing it in an array
{
Address = (
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 29;
"address_title" = "";
"building_name" = "";
latitude = "23.95579108";
longitude = "77.64169808";
"user_id" = 13;
},
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 31;
"address_title" = "";
"building_name" = "";
latitude = "22.95578162";
longitude = "77.64173089";
"user_id" = 13;
},
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 37;
"address_title" = "";
"building_name" = "";
latitude = "22.95577373";
longitude = "77.64173507";
"user_id" = 13;
},
{
address = "256, Road Number 19, Wadla Village, Vadala, Mumbai, Maharashtra 400031, India";
"address_id" = 49;
"address_title" = dsa;
"building_name" = das;
latitude = "19.01761470";
longitude = "72.85616440";
"user_id" = 13;
}
);
}
我正在尝试从 JSON 词典中检索数据。
我需要从 Address 字典中获取 address 中的数据并将其存储在一个数组中。
我的阵列应该是这样的:
[Bengaluru, Karnataka 560008, India,Bengaluru, Karnataka 560008, India,Bengaluru, Karnataka 560008, India,256, Road Number 19, Wadla Village, Vadala, Mumbai, Maharashtra 400031, India]
需要帮助。
Filtering NSDictionary with predicate
您可以使用 NSPredicate 过滤 NSDictionary。
假设你的字典是 NSDictionary *dict
NSArray *resultArray = [[dict objectForKey:@"Address" allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key == %@)", @"address"]];
NSMutableArray *resultArray = [NSMutableArray new];
NSError *jsonError = [[NSError alloc] init];
NSDictionary *pR = [NSJSONSerialization JSONObjectWithData:yourJsonObject options:NSJSONReadingAllowFragments error:&jsonError];
NSArray *pArray = pR[@"Address"];
for(NSDictionary *dict in pR)
{
[resultArray addObject:dict[@"address"]];
}
{
Address = (
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 29;
"address_title" = "";
"building_name" = "";
latitude = "23.95579108";
longitude = "77.64169808";
"user_id" = 13;
},
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 31;
"address_title" = "";
"building_name" = "";
latitude = "22.95578162";
longitude = "77.64173089";
"user_id" = 13;
},
{
address = "Bengaluru, Karnataka 560008, India";
"address_id" = 37;
"address_title" = "";
"building_name" = "";
latitude = "22.95577373";
longitude = "77.64173507";
"user_id" = 13;
},
{
address = "256, Road Number 19, Wadla Village, Vadala, Mumbai, Maharashtra 400031, India";
"address_id" = 49;
"address_title" = dsa;
"building_name" = das;
latitude = "19.01761470";
longitude = "72.85616440";
"user_id" = 13;
}
);
}
我正在尝试从 JSON 词典中检索数据。 我需要从 Address 字典中获取 address 中的数据并将其存储在一个数组中。 我的阵列应该是这样的: [Bengaluru, Karnataka 560008, India,Bengaluru, Karnataka 560008, India,Bengaluru, Karnataka 560008, India,256, Road Number 19, Wadla Village, Vadala, Mumbai, Maharashtra 400031, India]
需要帮助。
Filtering NSDictionary with predicate
您可以使用 NSPredicate 过滤 NSDictionary。
假设你的字典是 NSDictionary *dict
NSArray *resultArray = [[dict objectForKey:@"Address" allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key == %@)", @"address"]];
NSMutableArray *resultArray = [NSMutableArray new];
NSError *jsonError = [[NSError alloc] init];
NSDictionary *pR = [NSJSONSerialization JSONObjectWithData:yourJsonObject options:NSJSONReadingAllowFragments error:&jsonError];
NSArray *pArray = pR[@"Address"];
for(NSDictionary *dict in pR)
{
[resultArray addObject:dict[@"address"]];
}