【问题标题】:How do I fetch JSON data from a web service? iOS Objective-c如何从 Web 服务获取 JSON 数据? iOS 目标-c
【发布时间】:2013-11-30 21:45:01
【问题描述】:

我现在找不到有用的示例。我的服务器上有一个 php 脚本,它从选定的数据库表中返回所有数据。我正在尝试从我的客户端 ios 设备中获取它并解析该 json,以便我可以查看那里的内容并将其中一些数据添加到我的 sqlite 数据库中。我现在正在使用 fmdb 作为包装器。这是我连接到服务器的代码,我不确定从那里去哪里

  NSURL *url = [NSURL URLWithString:@"http://index.php"];

NSString * post =[NSString stringWithFormat:@"lang=%@",@"English"];

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setTimeoutInterval:60];

[request setURL:url];
[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

我的json数据是这样的

{"login":[{"userid":"1","password":"test","aclevel":"1"}],"answers":[{"aid":"1","qid":"1","sid":"1","freetext":null,"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null,"h":null,"i":null,"j":null,"synch":null,"type":null},{"aid":"5","qid":"5","sid":"5","freetext":null,"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null,"h":null,"i":null,"j":null,"synch":null,"type":null}],"projects":{"pid":"3","name":"apmatt","descr":"tapest","creator":"macurlatt","datetime":null,"synch":null},"questions":[{"qid":"1","pid":"1","sid":"1","question":null,"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null,"h":null,"i":null,"j":null,"synch":null,"type":null}],"surveys":[{"sid":"2","pid":"2","name":"matt","descr":"test","creator":"matt","datetime":null,"synch":null},{"sid":"3","pid":"3","name":"amatt","descr":"taest","creator":"maatt","datetime":null,"synch":null}]}

【问题讨论】:

    标签: php ios objective-c json sqlite


    【解决方案1】:

    如果您确定服务器返回的 JSON 非常有效,那就很简单了。 iOS 提供了NSJSONSerialization 类来让你实现这一点。以下代码行让您了解该过程:

    __block id data = nil;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse*       response, NSData* responseData, NSError* err) {
        data = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    }];
    

    如果响应采用有效的 JSON 格式,您可以将数据返回到“data”变量中,根据数据的格式,该变量可以是 NSArray 或 NSDictionary,它已被解析。

    【讨论】:

    • 我在数据行的上述代码中得到变量不可赋值。任何建议
    • @WilliamBrasky 没找到你。你说的变量是什么意思?您可以在responseData 中接收任何内容,并且代码应该可以正常工作,直到您获得一些有效的 JSON。
    • 那是因为你正在为块外定义的变量赋值。将第一行更改为 __block id data = nil;
    • 非常感谢。我的愚蠢错误。我已经编辑了我的答案。
    • 我不觉得您使用 POST 做错了什么,而且它也与服务器端数据库更改无关。在您的 myData 变量包含一些垃圾之前,您可以安全地发布。但是,在使用 post 变量之前,请考虑对您的 post 变量进行 url 编码 NSString *post =[[NSString stringWithFormat:@"any-data=%@",myData] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多