【问题标题】:How to print the json data如何打印json数据
【发布时间】: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


【解决方案1】:

根据您的问题,以下是我非常容易理解和基本的编码。它对您有帮助。

     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);
     }

【讨论】:

    【解决方案2】:

    打印是指在控制台中显示数据?

    你试过了吗?

    NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Data: %@", str);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多