Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Ios 如何返回多个JSON结果_Ios_Objective C_Json - Fatal编程技术网

Ios 如何返回多个JSON结果

Ios 如何返回多个JSON结果,ios,objective-c,json,Ios,Objective C,Json,我构建了一个web服务,它连接到我的Sql DB,并通过JSON返回请求的数据量 如果它返回一条记录,一切都很好,但是如果它返回多条记录,整个事情就会在for循环中消失。有谁能给我指出正确的方向,让我明白它为什么要这样做 NSString *urlString; urlString = @"http://url.php"; NSData *jsonSource = [NSData dataWithContentsOfURL: [NSURL URLWi

我构建了一个web服务,它连接到我的Sql DB,并通过JSON返回请求的数据量

如果它返回一条记录,一切都很好,但是如果它返回多条记录,整个事情就会在for循环中消失。有谁能给我指出正确的方向,让我明白它为什么要这样做

NSString *urlString;
urlString = @"http://url.php";

NSData *jsonSource = [NSData dataWithContentsOfURL:
                      [NSURL URLWithString:urlString]];

id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
    NSString *idData = [dataDict objectForKey:@"ID"];
 dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                  idData, @"ID",
 Nil];
 [myObject addObject:dictionary];
我试图将这些数据放入表视图中

JSON是:

[
    {
        "ID": "8",
        "APP_ID": "xx",
        "NAME": "xxx",
        "PRICING": "paid",
        "PRODUCT_ID": "xxx",
        "TITLE‌​": "xxx",
        "INFO": "xxx",
        "DATE": "2014-01-01 00:00:00",
        "AVAILABILITY": "published",
        "COVER": "xxxx",
        "URL": "xxxx",
        "ITUNES_SUMMARY‌​": "In this issue we interview Steve Jobs on all things Apple.",
        "ITUNES_COVERART_URL": "xxx",
        "ITUNES_PUBLISHED": "2012-11-01T00:00:00-07:0‌​0",
        "ITUNES_UPDATED": "2012-11-01T00:00:00-07:00"
    }
]
以下是生成JSON输出的PHP:

if($result = $con->query("SELECT * FROM ISSUES WHERE PRICING = 'free'")) { 
    $tempArray = array(); 
    while($row = $result->fetch_object()) { 
        $tempArray = $row; 
        array_push($json, $tempArray); 
        echo json_encode($json);
    }
 }

您确定响应是有效的JSON吗?将JSON数据转换为字符串并将其打印到控制台

NSLog(@"%@", [[NSString alloc] initWithData:jsonSource encoding:NSUTF8StringEncoding]);
。。。然后在JSON验证器(如或)中检查它

您还可以在
NSJSONSerialization
方法中传递指向
NSError
对象的指针。也将此记录到控制台

NSError *error;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource
                                                 options:NSJSONReadingMutableContainers
                                                   error:&error];
NSLog(@"error %@", error);
如果没有任何错误消息,那么您应该检查的最后一件事是生成的
jsonObjects
变量是您期望的类型(我假设您期望的是
NSArray


看到PHP代码后,这应该可以解决问题:

if($result = $con->query("SELECT * FROM ISSUES WHERE PRICING = 'free'")) { 
    $tempArray = array(); 
    while($row = $result->fetch_object()) {
        array_push($row, $tempArray); 
    }
    echo json_encode($tempArray);
}

您确定响应是有效的JSON吗?将JSON数据转换为字符串并将其打印到控制台

NSLog(@"%@", [[NSString alloc] initWithData:jsonSource encoding:NSUTF8StringEncoding]);
。。。然后在JSON验证器(如或)中检查它

您还可以在
NSJSONSerialization
方法中传递指向
NSError
对象的指针。也将此记录到控制台

NSError *error;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource
                                                 options:NSJSONReadingMutableContainers
                                                   error:&error];
NSLog(@"error %@", error);
如果没有任何错误消息,那么您应该检查的最后一件事是生成的
jsonObjects
变量是您期望的类型(我假设您期望的是
NSArray


看到PHP代码后,这应该可以解决问题:

if($result = $con->query("SELECT * FROM ISSUES WHERE PRICING = 'free'")) { 
    $tempArray = array(); 
    while($row = $result->fetch_object()) {
        array_push($row, $tempArray); 
    }
    echo json_encode($tempArray);
}

似乎您的服务器只是连接了JSON数组

[ { "ID":"1", ... } ] [ { "ID":"2", ... } ] ...
这是无效的JSON,
NSJSONSerialization
将在读取第一个JSON后停止 JSON块,将其余部分视为垃圾

服务器必须返回包含多个字典的数组:

[ { "ID":"1", ... }, { "ID":"2", ... }, ... ]

似乎您的服务器只是连接了JSON数组

[ { "ID":"1", ... } ] [ { "ID":"2", ... } ] ...
这是无效的JSON,
NSJSONSerialization
将在读取第一个JSON后停止 JSON块,将其余部分视为垃圾

服务器必须返回包含多个字典的数组:

[ { "ID":"1", ... }, { "ID":"2", ... }, ... ]

它是怎么死的?显示两种情况下的JSON。它并没有消亡,它正在等待完成。在这里尝试使用块进行异步下载请求猜测,但NSJSONSerialization很可能返回的类型是您不期望的@不过,韦恩是对的。需要查看返回的JSON。以下是单曲的JSON:[{“ID”:“8”,“APP_ID”:“xx”,“NAME”:“xxx”,“PRICING”:“paid”,“PRODUCT_ID”:“xxx”,“TITLE”:“xxx”,“INFO”:“xxx”,“DATE”:“2014-01-01 00:00:00”,“AVAILABILITY”:“published”,“COVER”:“xxxx”,“URL”:“xxxx”,“ITUNES\u SUMMARY”:“在本期中,我们就苹果的一切采访史蒂夫•乔布斯。”,“ITUNES覆盖范围URL”:“xxx”,“ITUNES发布”:“2012-11-01T00:00:00-07:00”,“ITUNES更新”:“2012-11-01T00:00:00-07:00”}]你能发布JSON吗?它是如何消亡的?显示这两种情况下的JSON。它没有消亡,它正在等待完成。尝试使用块在此处进行异步下载请求猜测,但很可能NSJSONSerialization返回的是一种你不期望的类型。@Wain是对的。但需要查看返回的JSON。这是您的JSON单曲:[{“ID”:“8”,“APP_ID”:“xx”,“NAME”:“xxx”,“PRICING”:“paid”,“PRODUCT_ID”:“xxx”,“TITLE”:“xxx”,“INFO”:“xxx”,“DATE”:“2014-01-01 00:00:00”,“AVAILABILITY”:“published”,“COVER”:“xxxx”,“URL”:“xxxx”,“ITUNES_摘要”:“在本期中,我们就苹果的一切采访史蒂夫·乔布斯”,“ITUNES_封面”;“xxx”,“ITUNES_发布”:“2012-11-01T00:00:00-07:00“,”ITUNES_UPDATED:““2012-11-01T00:00:00-07:00”}]你能发布JSON吗?好的,在错误日志的末尾我得到了一个垃圾。在你给出的第一行代码中,我还得到了一个NSString的不可见接口。很抱歉出现了“不可见接口”错误,我试图从内存中编写该方法。更新了我的答案。你说的“垃圾”是什么意思?垃圾角色,或者…?我想我明白你的问题了。看起来您的web服务不是返回单个资产数组,而是返回一组数组,每个数组有一个对象(并且没有任何东西将它们分开)。这是无效的JSON,这就是为什么
NSJSONSerialization
失败的原因。好的,在错误日志的末尾,我得到了一个垃圾。在您给出的第一行代码中,我还得到了NSString的一个不可见接口。对于“不可见接口”错误,我感到抱歉,我试图从内存中编写该方法。更新了我的答案。你说的“垃圾”是什么意思?垃圾角色,或者…?我想我明白你的问题了。看起来您的web服务不是返回单个资产数组,而是返回一组数组,每个数组有一个对象(并且没有任何东西将它们分开)。这是无效的JSON,这就是
NSJSONSerialization
失败的原因。