ios - CJSON反序列化错误

标签 ios objective-c json touchjson

我正在开发一个 JSON 解析应用程序。我正在使用 touchjson 来解析 json url,它是——

https://dl.dropboxusercontent.com/u/746330/facts.json

{
"title":"About Canada",
"rows":[
    {
    "title":"Beavers",
    "description":"Beavers are second only to humans in their ability to manipulate and change their environment. They can measure up to 1.3 metres long. A group of beavers is called a colony",
    "imageHref":"http://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/American_Beaver.jpg/220px-American_Beaver.jpg"
    },
    {
    "title":"Flag",
    "description":null,
    "imageHref":"http://images.findicons.com/files/icons/662/world_flag/128/flag_of_canada.png"
    },
    {
    "title":"Transportation",
    "description":"It is a well known fact that polar bears are the main mode of transportation in Canada. They consume far less gas and have the added benefit of being difficult to steal.",
    "imageHref":"http://1.bp.blogspot.com/_VZVOmYVm68Q/SMkzZzkGXKI/AAAAAAAAADQ/U89miaCkcyo/s400/the_golden_compass_still.jpg"
    },
    {
    "title":"Hockey Night in Canada",
    "description":"These Saturday night CBC broadcasts originally aired on radio in 1931. In 1952 they debuted on television and continue to unite (and divide) the nation each week.",
    "imageHref":"http://fyimusic.ca/wp-content/uploads/2008/06/hockey-night-in-canada.thumbnail.jpg"
    },
    {
    "title":"Eh",
    "description":"A chiefly Canadian interrogative utterance, usually expressing surprise or doubt or seeking confirmation.",
    "imageHref":null
    },
    {
    "title":"Housing",
    "description":"Warmer than you might think.",
    "imageHref":"http://icons.iconarchive.com/icons/iconshock/alaska/256/Igloo-icon.png"
    },
    {
    "title":"Public Shame",
    "description":" Sadly it's true.",
    "imageHref":"http://static.guim.co.uk/sys-images/Music/Pix/site_furniture/2007/04/19/avril_lavigne.jpg"
    },
    {
    "title":null,
    "description":null,
    "imageHref":null
    },
    {
    "title":"Space Program",
    "description":"Canada hopes to soon launch a man to the moon.",
    "imageHref":"http://files.turbosquid.com/Preview/Content_2009_07_14__10_25_15/trebucheta.jpgdf3f3bf4-935d-40ff-84b2-6ce718a327a9Larger.jpg"
    },
    {
    "title":"Meese",
    "description":"A moose is a common sight in Canada. Tall and majestic, they represent many of the values which Canadians imagine that they possess. They grow up to 2.7 metres long and can weigh over 700 kg. They swim at 10 km/h. Moose antlers weigh roughly 20 kg. The plural of moose is actually 'meese', despite what most dictionaries, encyclopedias, and experts will tell you.",
    "imageHref":"http://caroldeckerwildlifeartstudio.net/wp-content/uploads/2011/04/IMG_2418%20majestic%20moose%201%20copy%20(Small)-96x96.jpg"
    },
    {
    "title":"Geography",
    "description":"It's really big.",
    "imageHref":null
    },
    {
    "title":"Kittens...",
    "description":"Éare illegal. Cats are fine.",
    "imageHref":"http://www.donegalhimalayans.com/images/That%20fish%20was%20this%20big.jpg"
    },
    {
    "title":"Mounties",
    "description":"They are the law. They are also Canada's foreign espionage service. Subtle.",
    "imageHref":"http://3.bp.blogspot.com/__mokxbTmuJM/RnWuJ6cE9cI/AAAAAAAAATw/6z3m3w9JDiU/s400/019843_31.jpg"
    },
    {
    "title":"Language",
    "description":"Nous parlons tous les langues importants.",
    "imageHref":null
    }
]
}

出于某种原因,当我使用 CJONDeserializer 时出现错误 ---

Error Domain=CJSONDeserializerErrorDomain Code=-104 "Could not scan dictionary. Failed to scan a value." UserInfo=0x7feb38d99ed0 {NSLocalizedDescription=Could not scan dictionary. Failed to scan a value., line=0, character=0, location=0, snippet=!HERE>!{ "title":"About Can}



如果我使用 NSJSONSerializer 它会给我以下错误---

"Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 2643.) UserInfo=0x7f8559c56700 {NSDebugDescription=Unable to convert data to string around character 2643.}”



对于我正在使用的代码,如果我使用任何其他 json url,它不会给我任何错误。

这是我正在使用的代码 -
- (void)viewDidLoad {
NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        self.receivedData = [[NSMutableData alloc] init];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now

    NSError *error = nil;

    NSDictionary *receivedDataDictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:self.receivedData error:&error];

    NSArray *arrayFromJson = [receivedDataDictionary objectForKey:@"title"];

    for (NSDictionary *tempDict in arrayFromJson) {
        NSLog(@"blah %@", tempDict);
    }

}

任何帮助都感激不尽。提前致谢。

最佳答案

JSON 中的第一件事:“title”不包含数组,数组在关键“rows”中。

第二:看起来问题出在您的 JSON 中。您的 JSON 在倒数第三个对象中包含“É”。 CJSONDeserializer无法解码这种字符。使用NSJSONSerialization .试试下面的代码。

NSError *error;
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlString] encoding:NSISOLatin1StringEncoding error:&error];

NSData *resData = [string dataUsingEncoding:NSUTF8StringEncoding];

id jsonObject = [NSJSONSerialization JSONObjectWithData:resData options:kNilOptions error:&error];

if (error) {
    //Error handling
} else {
    //use your json object
     NSArray *arrayFromJson = [jsonObject objectForKey:@"rows"];

    for (NSDictionary *tempDict in arrayFromJson) {
         NSLog(@"blah %@", tempDict);
    }
}

关于ios - CJSON反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025917/

相关文章:

ios - 在 Xcode 中进行 UI 布局时 View 的框架变为 (0,0,0,0) 的奇怪错误?

iphone - UIImagePickerController 在 iPad 中崩溃

iphone - iOS 4上的人脸检测

json - 榆树 'Json.Decode.succeed' : how is it used in a decode pipeline if it is supposed to always return the same value?

ios - Swift:xib文件的继承

ios - 合并具有不同分辨率的剪辑

ios - Realm swift : All records between specific time

ios - Today Widget 在 iOS 8 设备上没有内容

具有内部数组转换的 JSON 到字符串

ios - Swift JSONDecoder - 编码键不适用于下划线