NSJSONSerialization不起作用,NSUTF8StringEncoding起作用 [英] NSJSONSerialization Not Working, NSUTF8StringEncoding Is Working

查看:116
本文介绍了NSJSONSerialization不起作用,NSUTF8StringEncoding起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,教程和其他Stack Q& A,并迷恋如何使用NSUTF8StringEncoding而不是NSJSONSerialization来显示我的JSON-我需要将数据提取到NSDictionaryNSArray.我认为从我的URL返回NSData的格式有问题吗?

I've read the documentation, tutorials, and other Stack Q&A and am hung up on how I'm able to get my JSON to appear using NSUTF8StringEncoding, but not using NSJSONSerialization - which is what I need to extract the data to a NSDictionary or NSArray. I believe it is an issue with the format of the NSData being returned from my URL?

EDIT 这是错误:Error Domain = NSCocoaErrorDomain代码= 3840操作无法完成.(可可错误3840.)"(结束时是垃圾.)UserInfo = 0x8aabc30

EDIT Here is error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=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)

推荐答案

您的JSON格式不正确.您告诉我们这是(为清晰起见添加了空格和换行符):

Your JSON is not well formed. You tell us that it is (white space and line breaks added for legibility purposes):

{
    "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的[].如果它确实是字典条目的数组,则应为:

That's missing a comma between the two dictionary entries, and you're missing the [ and ] that should wrap the JSON. If it's really an array of dictionary entries, it should be:

[
    {
        "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,可以访问 http://jsonlint.com .另外,如果您检查了JSONObjectWithData返回的error对象,它会为您指明正确的方向.

If you want to check your JSON, you can visit http://jsonlint.com. Also, if you checked the error object returned by JSONObjectWithData, it would have pointed you in the right direction.

您应该查看如何生成此JSON.看起来它必须是手动生成的.例如,如果您是从PHP页面生成的,则应使用 json_encode ,而不是手动生成JSON.

You should look how this JSON was generated. It looks like it must have been manually generated. If you were generating this from a PHP page, for example, you should use json_encode, for example, rather than manually generating the JSON.

顺便说一句,您正在尝试解析didReceiveData中的结果.在connectionDidFinishLoading之前,您不应该尝试解析.您的didReceiveData应该仅将data附加到您的NSMutableData对象.解析应该推迟到connectionDidFinishLoading.

By the way, you are trying to parse the result in didReceiveData. You shouldn't try to parse until connectionDidFinishLoading. Your didReceiveData should merely append the data to your NSMutableData object. The parsing should be deferred until connectionDidFinishLoading.

这篇关于NSJSONSerialization不起作用,NSUTF8StringEncoding起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆