首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Cocoa-Touch -如何解析本地Json文件

Cocoa-Touch -如何解析本地Json文件
EN

Stack Overflow用户
提问于 2012-08-28 06:41:44
回答 4查看 19.9K关注 0票数 13

我是Json开发人员的新手,我正在尝试解析本地iOS文件,例如

{"quizz":[{"id":"1","Q1":"When Mickey was born","R1":"1920","R2":"1965","R3":"1923","R4","1234","response","1920"},{"id":"1","Q1":"When start the cold war","R1":"1920","R2":"1965","R3":"1923","rep4","1234","reponse","1920"}]}

下面是我的代码:

代码语言:javascript
运行
复制
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
// Parse the string into JSON
NSDictionary *json = [myJSON JSONValue];

// Get all object
NSArray *items = [json valueForKeyPath:@"quizz"];

NSEnumerator *enumerator = [items objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) {
    NSLog(@"clientId = %@",  [item objectForKey:@"id"]);
    NSLog(@"clientName = %@",[item objectForKey:@"Q1"]);
    NSLog(@"job = %@",       [item objectForKey:@"Q2"]);
}

我在这个网站上找到了一个示例,但我得到了以下错误

-JSONValue失败。错误是:对象键后不应有标记“值分隔符”。

EN

回答 4

Stack Overflow用户

发布于 2014-06-30 18:25:08

代码语言:javascript
运行
复制
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"json"];
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSError *error =  nil;
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
票数 4
EN

Stack Overflow用户

发布于 2012-08-28 06:56:22

使用jsonlint.com查找JSON字符串中的错误。

在本例中,它显示在"R4"附近有无效的JSON

票数 2
EN

Stack Overflow用户

发布于 2012-08-28 06:55:43

您的json文件中似乎有一个拼写错误。

替换

使用"R4":"1234","response":"1920""R4","1234","response","1920"

使用"rep4":"1234","response":"1920""rep4","1234","reponse","1920"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12150595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档