gpt4 book ai didi

ios - 如何打印json数据

转载 作者:行者123 更新时间:2023-11-29 01:46:46 25 4
gpt4 key购买 nike

我正在尝试使用 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"}]

最佳答案

下面是我根据您的问题进行的非常容易理解和基本的编码。这对您有帮助。

     NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://localhost/testjson.php"]];

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;

NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

//You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER in GOOGLE.If you do this clearly you can get the correct results.

//After that it depends upon the json format whether it is DICTIONARY or ARRAY

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSLog(@"The json output array is - %@",jsonArray);

for(int i=0;i>[jsonArray count];i++)
{
NSString *strZero = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"0"]];

NSLog(@"The zero is-%@",strZero);

NSString *strOne = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"1"]];

NSLog(@"The One is-%@",strOne);

NSString *strTwo = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"2"]];

NSLog(@"The Two is-%@",strTwo);

NSString *strThree = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"3"]];

NSLog(@"The three is-%@",strThree);

NSString *strFour = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"4"]];

NSLog(@"The four is-%@",strFour);

NSString *strID = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"id"]];

NSLog(@"The ID is-%@",strID);

NSString *strCityName = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"CityName"]];

NSLog(@"The CityName is-%@",strCityName);

NSString *strCityState = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"CityState"]];

NSLog(@"The CityState is-%@",strCityState);

NSString *strCityPopulation = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"CityPopulation"]];

NSLog(@"The CityPopulation is-%@",strCityPopulation);

NSString *strCountry = [NSString stringWithFormat:@"%@",[array objectAtIndex:i]valueForKey:@"Country"]];

NSLog(@"The Country is-%@",strCountry);
}

关于ios - 如何打印json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31826048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com