iOS:用逗号分隔的JSON解析

时间:2013-04-24 03:36:38

标签: objective-c json parsing nsdictionary

以下是从Google地方信息自动填充API获取NSDictionary的代码:

(GooglePlacesAutocompletePlace *)placeFromDictionary:(NSDictionary *)placeDictionary 
{
    GooglePlacesAutocompletePlace *place = [[self alloc] init];
    place.name = [placeDictionary objectForKey:@"description"];
    return place;
}

上面的代码将以下内容存储在place.name中:

"Name: Apple Store Palo Alto, University Avenue, Palo Alto, CA, United States" 

结果以逗号分隔。在将其存储在place.name之前删除Country的名称的最佳方法是什么。我想只显示州的地址。

1 个答案:

答案 0 :(得分:1)

你可以使用rangeOfString:options:range和NSBackwardsSearch选项找到最后一个逗号:

    NSString *s = @"Name: Apple Store Palo Alto, University Avenue, Palo Alto, CA, United States";
    NSRange lastCommaRange = [s rangeOfString:@"," options:NSBackwardsSearch range:NSMakeRange(0, s.length)];
    NSString *noCountryString = [s substringToIndex:lastCommaRange.location];
    NSLog(@"%@",noCountryString);