解析省市的Jason文件

来源:互联网 发布:js将数据导出成excel 编辑:程序博客网 时间:2024/05/15 12:14

- (NSArray *)getCityStr:(NSString *)strCityID

{

    NSString *strCity = nil;

    NSString *strPro = nil;

    

    NSString *strPath = [[[NSBundlemainBundle] resourcePath] stringByAppendingPathComponent: @"citys2.json"];

    if (strPath && strPath.length)

    {

        NSData *data = [NSDatadataWithContentsOfFile: strPath];

        

        if (data)

        {

            id object = [data objectFromJSONData];

            if (object && [object isKindOfClass:[NSArray class]])

            {

                NSArray *dataArray = (NSArray*)object;

                if (dataArray && [dataArraycount])

                {

                    for (int i=0; i<dataArray.count; i++)

                    {

                        NSLog(@"city %@ cityid %@  proid%@",[[[dataArrayobjectAtIndex:i] objectAtIndex:0]objectForKey:@"city"],[[[dataArrayobjectAtIndex:i] objectAtIndex:0]objectForKey:@"cityID"],[[[dataArrayobjectAtIndex:i] objectAtIndex:0]objectForKey:@"provinceID"]);

                        

                        NSRange range = [strCityIDrangeOfString:[[[dataArray objectAtIndex:i]objectAtIndex:0] objectForKey:@"cityID"]];

                        if (range.location!=NSNotFound)

                        {

                            strCity = [[[dataArray objectAtIndex:i] objectAtIndex:0]objectForKey:@"city"];

                            strPro = [[[dataArray objectAtIndex:i] objectAtIndex:0]objectForKey:@"provinceID"];

                            NSLog(@"%@ %@",strCity,strPro);

                            break ;

                        }

                    }

                    

                }

                

            }

        }

    }


    NSArray *arr = [[NSArrayalloc]initWithObjects:strPro,strCity,nil];

    return [arr autorelease];

}

原创粉丝点击