Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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返回的json的Xcode解析错误_Php_Ios_Mysql_Objective C_Json - Fatal编程技术网

从php返回的json的Xcode解析错误

从php返回的json的Xcode解析错误,php,ios,mysql,objective-c,json,Php,Ios,Mysql,Objective C,Json,我正在尝试使用PHP从MySQL服务器获取数据。以下是PHP文件内容: <?php header('Content-type: application/json; charset=utf-8'); $dbc=mysql_connect("host", "defaultuser", "defaultuser"); mysql_select_db("customer"); $query = "SELECT * FROM customer_infor"; $result = mysql_que

我正在尝试使用PHP从MySQL服务器获取数据。以下是PHP文件内容:

<?php 
header('Content-type: application/json; charset=utf-8');
$dbc=mysql_connect("host", "defaultuser", "defaultuser");
mysql_select_db("customer");
$query = "SELECT * FROM customer_infor";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result))
{
$id_index=$line['id_index'];
$first_name=$line['first_name'];
$last_name=$line['last_name'];

$customers[]=array('id_index'=>$id_index, 'first_name'=>$first_name, 'last_name'=>$last_name);

}
$response['customers']=$customers;

echo json_encode($response);
mysql_close($dbc);
?>
当我尝试使用Xcode接收json字符串并对其进行解析时,请使用以下代码:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.connectionData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *retString = [[NSString alloc] initWithData:self.connectionData encoding:NSUTF8StringEncoding];    

    NSError *parseError = nil;

    self.dataArray = [NSJSONSerialization JSONObjectWithData:self.connectionData options:0 error:&parseError];
    if (!parseError) {

        NSLog(@"json array is %@", self.dataArray);
        [self.tableView reloadData];
    } else {
        NSString *err = [parseError localizedDescription];
        NSLog(@"Encountered error parsing: %@", err);
    }  
    connection = nil;
    self.connectionData = nil;
}
此Xcode脚本位于视图控制器中。返回json数据时,首先将其放入
self.connectionData
,然后由
NSJSONSerialization
解析。数据最终放入
self.dataArray
。 我原以为dataArray中有3个元素,用于这三个客户。但是,
NSLog
和系统调试都只指示阵列中的一个元素。此错误阻止我进一步将客户数据放入表视图

有人能帮我找到这个问题的解决方案吗?

试试这个:

NSDictionary *dataReturned = [NSJSONSerialization JSONObjectWithData:self.connectionData options:0 error:&parseError];
self.dataArray = dataReturned[@"customers"];
if (!parseError) {

    NSLog(@"json array is %@", self.dataArray);
    [self.tableView reloadData];
} else {
    NSString *err = [parseError localizedDescription];
    NSLog(@"Encountered error parsing: %@", err);
}  

希望这有帮助:)

self.dataArray
是一个字典,它的
valueForKey:@“customers”
是一个数组。或者它的
objectForKey
。或者
self.dataArray[@“customers”]
。Xcode只是一个奇特的编辑器/编译器。这是你的应用程序,做上述工作。非常感谢男人!它工作完美!NSDictionary和NSArray总是让我困惑。。。。。
NSDictionary *dataReturned = [NSJSONSerialization JSONObjectWithData:self.connectionData options:0 error:&parseError];
self.dataArray = dataReturned[@"customers"];
if (!parseError) {

    NSLog(@"json array is %@", self.dataArray);
    [self.tableView reloadData];
} else {
    NSString *err = [parseError localizedDescription];
    NSLog(@"Encountered error parsing: %@", err);
}