ios - 如何打印json数据

标签 ios objective-c json xcode xcode6

我正在尝试使用 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/

相关文章:

ios - 双击无法缩放图像

json - 在 Node + express + Jade 中将对象传递给客户端?

android - 发布版本中的空 json 数据(使用混淆器)

ios - 在 Swift 中使用 Realm

ios - subview.origin 可以使用负坐标吗?

ios - Storyboard View 元素变灰

iphone - 在 TableView 中保存和加载 UISwitch 状态

c# - 关于如何在 Try/Catch 异常后忽略 for 循环中的文件的任何建议?

android - BTLE(低功耗蓝牙)开发套件 - 必须具有接近配置文件

android - 即使应用程序已关闭或最小化, ionic 推送通知是否有效?