我很难在 iOS 5 上解析下面的 JSON 字符串。
{"States": [{"Name": "Arizona","Cities": [{"Name": "Phoenix"}]},{"Name": "California","Cities": [{"Name": "Orange County"},{"Name": "Riverside"},{"Name": "San Diego"},{"Name": "San Francisco"}]},{"Name": "Nevada","Cities": [{"Name": "Las Vegas"}]}]}
这是我的代码:
- (void) parseJson {
NSError *jsonError = nil;
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]];
if (jsonData) {
NSDictionary *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
return;
}
NSLog(@"%@", jsonObjects);
}
}
我不断收到此错误:
JSON Error: The operation couldn’t be completed. (Cocoa error 3840.)
我希望得到一些帮助,因为我显然无法解决这个问题。
让我觉得不正确的一件事是:
[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]
你的数据是RTF文件??它应该是一个txt
文件(或任何其他类型的纯文本文件)。 RTF 文件通常包含文本格式数据,如下所示:
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs24 \cf0 \{"States": [\{"Name": "Arizona","Cities": [\{"Name": "Phoenix"\}]\},\{"Name": "California","Cities": [\{"Name": "Orange County"\},\{"Name": "Riverside"\},\{"Name": "San Diego"\},\{"Name": "San Francisco"\}]\},\{"Name": "Nevada","Cities": [\{"Name": "Las Vegas"\}]\}]\}}
当我阅读时that作为数据并尝试将其解析为 JSON,我收到您所看到的 3840 错误。该错误的描述如下:
无法读取数据,因为数据已损坏。 (字符 2 周围的对象中没有值的字符串键。)
所以在我看来,你实际上并没有 JSON。您有 RTF 数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)