Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 NSJSONSerialization不工作,NSUTF8StringEncoding正在工作_Ios_Json_Nsdata_Nsjsonserialization - Fatal编程技术网

Ios NSJSONSerialization不工作,NSUTF8StringEncoding正在工作

Ios NSJSONSerialization不工作,NSUTF8StringEncoding正在工作,ios,json,nsdata,nsjsonserialization,Ios,Json,Nsdata,Nsjsonserialization,我已经阅读了文档、教程和其他堆栈问答,对于如何使用NSUTF8StringEncoding,而不是使用NSJSONSerialization,让我的JSON显示我的JSON,我感到很困惑,这正是我需要将数据提取到NSDictionary或NSArray中的原因。我认为这是从我的URL返回的NSData格式的问题 此处编辑错误:错误域=NSCOCAERRORDOMAIN代码=3840“操作无法完成。(Cocoa错误3840。)”(结尾为垃圾。)用户信息=0x8aabc30 NSURLRequest

我已经阅读了文档、教程和其他堆栈问答,对于如何使用
NSUTF8StringEncoding
,而不是使用
NSJSONSerialization
,让我的
JSON
显示我的
JSON
,我感到很困惑,这正是我需要将数据提取到
NSDictionary
NSArray
中的原因。我认为这是从我的URL返回的
NSData
格式的问题

此处编辑错误:错误域=NSCOCAERRORDOMAIN代码=3840“操作无法完成。(Cocoa错误3840。)”(结尾为垃圾。)用户信息=0x8aabc30

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sampleURL.com"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:15.0];

NSMutableData* responseData = [NSMutableData dataWithCapacity: 0];

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    //this works, and have NSLog below
    responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"string %@", responseString);

    [responseData appendData:data];
    responseData = [[NSMutableData alloc] initWithData:data];

     //this is null in NSLog
    NSError * error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data   options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];

    NSLog(@"jsonDict dict %@",jsonDict); 
}
调试控制台:

 2014-03-11 07:41:58.233 WBPC[7845:a0b] string   {"id":"a1","session":"General","name":"Exhibitor Setup Begins","startTime":"0900","details":"9am Exhibitor Hall","png":"image","speaker1":"Johnson","speaker2":"Nelson","speaker3":""}{"id":"b1","session":"General","name":"Conference Registration","startTime":"1000","details":"10am Noon Upper Level Lobby","png":"image","speaker1":"Jackson","speaker2":"","speaker3":""}
 2014-03-11 07:41:58.236 WBPC[7845:a0b] jsonDict (null)

这一切都是在“didReceiveData”委托函数中完成的,而该函数的位置不正确。唯一应该存在的是:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
}
但在此之前,您应该在连接收到响应时初始化“self.responseData”:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if(self.responseData != nil) self.responseData = nil;
    self.responseData = [[NSMutableData alloc] init];
}
然后,您需要实现另一个名为“ConnectionIDFinishLoading”的委托函数,当调用此方法时,“responseData”将包含JSON中的所有数据,并准备好解析:

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

     responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

     NSLog(@"string %@", responseString);

     NSError * error;
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];

     NSLog(@"jsonDict dict %@",jsonDict); 
}

我希望这对你有所帮助。

嗨,你可以试试这个

NSError *error = nil;
NSArray *jsonResponseArray = [NSJSONSerialization JSONObjectWithData: YOURDATA options: NSJSONReadingMutableContainers error: &error];

if (!jsonResponseArray) {
  NSLog(@"Error: %@", error);
} else {
   for(NSDictionary *dic in jsonResponseArray) {
      NSLog(@"dic values are : %@", item);
   }
}

谢谢

您发布的JSON字符串不是有效的JSON。仅仅因为数据是有效的UTF8字符串并不意味着它一定是JSON。尝试将调试后的字符串粘贴到中,它将显示JSON对象之间缺少逗号。

您的JSON格式不正确。您告诉我们它是(为了便于阅读,添加了空格和换行符):

这在两个字典条目之间缺少了一个逗号,并且缺少了应该包装JSON的
[
]
。如果它确实是一个字典条目数组,那么它应该是:

[
    {
        "id": "a1",
        "session": "General",
        "name": "Exhibitor Setup Begins",
        "startTime": "0900",
        "details": "9am Exhibitor Hall",
        "png": "image",
        "speaker1": "Johnson",
        "speaker2": "Nelson",
        "speaker3": ""
    },
    {
        "id": "b1",
        "session": "General",
        "name": "Conference Registration",
        "startTime": "1000",
        "details": "10am Noon Upper Level Lobby",
        "png": "image",
        "speaker1": "Jackson",
        "speaker2": "",
        "speaker3": ""
    }
]
如果您想检查JSON,可以访问。另外,如果您检查了由
JSONObjectWithData
返回的
error
对象,它将为您指明正确的方向

您应该看看这个JSON是如何生成的。看起来它一定是手动生成的。例如,如果您是从PHP页面生成此文件,那么您应该使用,而不是手动生成JSON



顺便说一下,您正试图在
didReceiveData
中解析结果。在
connectiondFinishLoading
之前,不应尝试解析。您的
didReceiveData
应该只将
data
附加到
NSMutableData
对象。解析应推迟到
connectiondFinishLoading

NSMutableDictionary*json=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:&错误];试试这个我真的试过那个(从你们中的一个其他堆栈帖子),但运气不好。我确信这是从URL返回JSON的格式问题…@NamoNamo-以下是我从NSJSONSerialization收到的错误:error Domain=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa错误3840.)”(垃圾在末尾。)UserInfo=0x8aabc30。谢谢。谢谢分享。实际上我确实使用了这些委托方法,我只是简化了我的示例以便于说明。除了上面已经显示的方法之外,在委托方法
连接:didReceiveResponse:
中,应该检查HTTP状态代码和响应数据的类型(MIME类型)。如果状态代码指示错误,我们应该读取响应,以便从服务器获取可能的“错误消息”。在
connectiondFinishLoading:
中,我们再次使用HTTP状态代码来决定是否应记录错误消息或根据MIME类型处理预期的响应数据。不幸的是,我也尝试了此方法,但没有成功。问题在于我的PHP如何输出JSON,我需要像这样把它包装成一个数组:$output=array();而($list=mysql\u fetch\u assoc($query)){$output[]=$list;}echo json\u encode($output);当然,JSON可以是UTF-16或UTF-32,所以有效的JSON不需要是有效的UTF8字符串。
[
    {
        "id": "a1",
        "session": "General",
        "name": "Exhibitor Setup Begins",
        "startTime": "0900",
        "details": "9am Exhibitor Hall",
        "png": "image",
        "speaker1": "Johnson",
        "speaker2": "Nelson",
        "speaker3": ""
    },
    {
        "id": "b1",
        "session": "General",
        "name": "Conference Registration",
        "startTime": "1000",
        "details": "10am Noon Upper Level Lobby",
        "png": "image",
        "speaker1": "Jackson",
        "speaker2": "",
        "speaker3": ""
    }
]