【发布时间】:2015-08-05 07:29:35
【问题描述】:
我正在尝试使用 IOS 学习 JSON,因为我是 IOS 设备的初学者,到目前为止我尝试了此代码
-(void)retriveData
{
NSURL *url = [NSURL URLWithString:@"http://localhost/testjson.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
cities = [[NSMutableArray alloc] init];
for (int i=0; i<json.count; i++) {
NSString *cID = [[json objectAtIndex:i] objectForKey:@"id"];
NSString *cName = [[json objectAtIndex:i] objectForKey:@"CityName"];
NSString *cState = [[json objectAtIndex:i] objectForKey:@"CityState"];
NSString *cPopulation = [[json objectAtIndex:i] objectForKey:@"CityPopulation"];
NSString *cCountry = [[json objectAtIndex:i] objectForKey:@"Country"];
Country *myCity = [[Country alloc] initWithCityID:cID andCityName:cName andCityState:cState andCityPopulation:cPopulation andCityCountry:cCountry];
[cities addObject:myCity];
}
}
现在谁能告诉我如何打印数据?这是json文件
[{"0":"1","id":"1","1":"Muscat","CityName":"Muscat","2":"Muscat","CityState":"Muscat","3":"25000","CityPopulation":"25000","4":"Oman","Country":"Oman"},{"0":"2","id":"2","1":"Bawsher","CityName":"Bawsher","2":"Muscat","CityState":"Muscat","3":"10000","CityPopulation":"10000","4":"Oman","Country":"Oman"},{"0":"3","id":"3","1":"AlMawalih","CityName":"AlMawalih","2":"Seeb","CityState":"Seeb","3":"5000","CityPopulation":"5000","4":"Oman","Country":"Oman"},{"0":"4","id":"4","1":"Oran","CityName":"Oran","2":"Oran","CityState":"Oran","3":"100000","CityPopulation":"100000","4":"Algeria","Country":"Algeria"},{"0":"5","id":"5","1":"Constantine","CityName":"Constantine","2":"Constantine","CityState":"Constantine","3":"150000","CityPopulation":"150000","4":"Algeria","Country":"Algeria"}]
【问题讨论】:
-
你的 json 是在数组中启动的,所以添加这个 NSArray *json
-
你想在哪里打印 json 数据?
-
我已经这样做了,谢谢,我只想在控制台中打印它
标签: ios objective-c json xcode xcode6