JSON解析:从NSDictionary中检索数据并将其存储在数组中

时间:2015-02-03 12:19:18

标签: ios json parsing nsdictionary

{
    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字典中获取地址中的数据并将其存储在数组中。 我的数组应如下所示: [班加罗尔,卡纳塔克邦560008,印度,班加罗尔,卡纳塔克邦560008,印度,班加罗尔,卡纳塔克邦560008,印度,256,路号19,瓦德拉村,瓦达拉,孟买,马哈拉施特拉邦400031,印度]

请帮助。

2 个答案:

答案 0 :(得分:0)

Filtering NSDictionary with predicate

您可以使用NSPredicate过滤NSDictionary。

假设您的词典是NSDictionary *dict

NSArray *resultArray = [[dict objectForKey:@"Address" allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key == %@)", @"address"]];

答案 1 :(得分:0)

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