Ios 调用Web服务时如何查看数据?

Ios 调用Web服务时如何查看数据?,ios,objective-c,json,web-services,Ios,Objective C,Json,Web Services,我最近一直在接受iOS培训,现在我正在尝试学习web服务。我相信我有一个主要的概念: 我打电话给一个网络服务 我从那个web服务接收数据 我解析JSON数据 我现在可以对数据做任何我想做的事情 现在,我已经阅读了许多关于一个简单web服务示例的教程,但它们都使用表视图来填充数据。在我更有经验之前,我不一定想这么做。现在,我只想打个电话,获取数据,然后记录下来。以下是我从教程中获得的一些代码: #import <Foundation/Foundation.h> #define WX

我最近一直在接受iOS培训,现在我正在尝试学习web服务。我相信我有一个主要的概念:

  • 我打电话给一个网络服务

  • 我从那个web服务接收数据

  • 我解析JSON数据

  • 我现在可以对数据做任何我想做的事情

现在,我已经阅读了许多关于一个简单web服务示例的教程,但它们都使用表视图来填充数据。在我更有经验之前,我不一定想这么做。现在,我只想打个电话,获取数据,然后记录下来。以下是我从教程中获得的一些代码:

#import <Foundation/Foundation.h>
#define WXFORECAST @"http://api.openweathermap.org/data/2.5/\
forecast/daily?q=%@&units=Imperial&cnt=7&mode=json"
#define LOCATION @"Fairbanks"

int main(int argc, const char * argv[])
{

@autoreleasepool {


    // insert code here...

    // Start the refresh control

    // Create the URL string based on location
    NSString *urlString =
    [NSString stringWithFormat:WXFORECAST, LOCATION];

    // Setup the session
    NSURLSessionConfiguration * configuration =
    [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session =
    [NSURLSession sessionWithConfiguration:configuration];
    NSURLRequest *request =
    [NSURLRequest requestWithURL:[NSURL
                                  URLWithString:urlString]];

    // Create a data task to transfer the web service endpoint contents
    NSURLSessionDataTask *dataTask =
    [session dataTaskWithRequest:request
               completionHandler:^(NSData *data,
                                   NSURLResponse *response, NSError *error) {

                   // Stop the refresh control

                   if (error)
                   {

                       return;
                   }

                   // Parse the JSON from the data object
                   NSDictionary *json = [NSJSONSerialization
                                         JSONObjectWithData:data options:0 error:nil];

                   // Store off the top level array of forecasts
                   NSMutableArray *items = [[NSMutableArray alloc] init];
                   items = json[@"list"];

                   }];

    // Start the data task
    [dataTask resume];

}
return 0;
}
#导入
#定义WXFORECAST@“http://api.openweathermap.org/data/2.5/\
预测/每日?q=%@&units=Imperial&cnt=7&mode=json“
#定义位置@“费尔班克斯”
int main(int argc,const char*argv[]
{
@自动释放池{
//在这里插入代码。。。
//启动刷新控件
//基于位置创建URL字符串
NSString*urlString=
[NSString stringWithFormat:WXFORECAST,位置];
//设置会话
NSURLSessionConfiguration*配置=
[NSURLSessionConfiguration默认会话配置];
NSURLSession*会话=
[NSURLSession sessionWithConfiguration:configuration];
NSURLRequest*请求=
[NSURLRequest requestWithURL:[NSURL]
URLWithString:urlString]];
//创建数据任务以传输web服务端点内容
NSURLSessionDataTask*数据任务=
[会话dataTaskWithRequest:请求
completionHandler:^(NSData*数据,
NSURLResponse*响应,NSError*错误){
//停止刷新控件
如果(错误)
{
返回;
}
//从数据对象解析JSON
NSDictionary*json=[NSJSONSerialization
JSONObjectWithData:数据选项:0错误:无];
//存储顶级预测数组
NSMutableArray*项=[[NSMutableArray alloc]init];
items=json[@“list”];
}];
//启动数据任务
[数据任务恢复];
}
返回0;
}
现在说我不知道这个代码是一种轻描淡写的说法。当我看到这一幕时,我不知所措。我相信这段代码正在进行调用,但现在我想记录来自该web服务的数据。这似乎是一个足够简单的答案,但在过去的几个小时里,我一直为此感到沮丧


感谢您提供的所有帮助。

除了按照bobnoble的建议执行NSLog之外,如果您在执行以下代码行后放置断点,您可以在调试器中打印NSDictionary的内容

      NSDictionary *json = [NSJSONSerialization
                                     JSONObjectWithData:data options:0 error:nil];

在上面的示例代码中,您将键入“po json”(不带单引号),这将输出整个字典。

您可以使用
NSLog
来显示响应,例如,
NSLog(@“%@”,json)