Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从web服务获取JSON数据?iOS目标-c_Php_Ios_Objective C_Json_Sqlite - Fatal编程技术网

Php 如何从web服务获取JSON数据?iOS目标-c

Php 如何从web服务获取JSON数据?iOS目标-c,php,ios,objective-c,json,sqlite,Php,Ios,Objective C,Json,Sqlite,我现在找不到一个有用的例子。我的服务器上有一个php脚本,它返回选定数据库表中的所有数据。我正试图从客户端ios设备获取这些数据,并解析这些json,这样我就可以查看其中的内容,并将其中的一些数据添加到我的sqlite数据库中。我现在正在使用fmdb作为包装器。这是我连接到服务器的代码,我不知道从那里去哪里 NSURL *url = [NSURL URLWithString:@"http://index.php"]; NSString * post =[NSString stringWit

我现在找不到一个有用的例子。我的服务器上有一个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}]}

如果您确定服务器返回的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,它已解析。

我在数据行的上述代码中得到了不可赋值的变量。任何suggestions@WilliamBrasky我不明白你的意思。你说的变量是什么意思?您可以在
responseData
中接收任何内容,并且代码应该一直工作,直到您获得一些有效的JSON。这是因为您正在为块外定义的变量赋值。将第一行更改为uu块id data=nil;谢谢。我犯了愚蠢的错误。我已经编辑了我的答案。我不觉得你使用POST做了什么错事,它与服务器端数据库的更改也没有任何关系。直到您的
myData
变量包含一些垃圾,您才可以安全地发布。但是,在使用它之前,请考虑URL对<代码> POST <代码>变量的编码:<代码> NScord*POST=[[NSnScRungFieldFrase:@ ]“任何数据=%@”,MyDATa] StrugBydAdEntAdvestuSuffsIn编码:NSUTF8STRIGDENTIGN);代码>