Cocoa-Touch - 如何解析本地Json文件 [英] Cocoa-Touch - How parse local Json file

查看:85
本文介绍了Cocoa-Touch - 如何解析本地Json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm newbie in iOS dev and I'm trying to parse a local Json file such as

{quizz:[ {id:1,Q1:当米奇出生,R1:1920,R2:1965,R3:1923,R4,1234 ,反应,1920},{id:1,Q1:开始冷战时,R1:1920,R2:1965 :1923,rep4,1234,reponse,1920}}}

 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"]);
}

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

I found on this site a sample but I get the following error

-JSONValue失败。错误是:对象键之后不会出现令牌值分隔符。

-JSONValue failed. Error is: Token 'value separator' not expected after object key.

推荐答案

JSON具有严格的键/ /值对,并且响应不正确。尝试此操作:

JSON has a strict key/Value notation, your key/value pairs for R4 and response are not correct. Try this:

NSString *jsonString = @"{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1965\",\"R3\":\"1923\",\"R4\":\"1234\",\"response\":\"1920\"}]}";

如果从文件中读取字符串,则不需要所有斜杠

您的档案会是这样:

If you read the string from a file, you don't need all the slashes
Your file would be something like this:


{quizz:[{id :1,Q1:当米奇是
出生,R1:1920,R2:1965,R3:1923,R4:1234 ,response:1920},{id:1,Q1:When
start the cold
war,R1:1920,R2 :1965,R3:1923,R4:1234,reponse:1920}]}

{"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","R4":"1234","reponse":"1920"}]}






我用这个代码测试:


I tested with this code:

NSString *jsonString = @"{\"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\",\"R4\":\"1234\",\"reponse\":\"1920\"}]}";
NSLog(@"%@", jsonString);
NSError *error =  nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

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"]);
}

我得到的印象是,你复制旧代码,苹果的序列化和枚举器,而不是快速枚举。整个枚举的东西可以写成简单的

I got the impression, that you copied old code, as you are not using apple's serialization and a Enumerator instead of Fast Enumeration. The whole enumeration stuff could be written simple as

NSArray *items = [json valueForKeyPath:@"quizz"];
for (NSDictionary *item in items) {
    NSLog(@"clientId = %@",  [item objectForKey:@"id"]);
    NSLog(@"clientName = %@",[item objectForKey:@"Q1"]);
    NSLog(@"job = %@",       [item objectForKey:@"Q2"]);
}

或甚至fancier与基于块的枚举,您还有另外一个

or even fancier with block based enumeration, hwere you have additionaly an index if needed to the fast and secure enumeration.

NSArray *items = [json valueForKeyPath:@"quizz"];
[items enumerateObjectsUsingBlock:^(NSDictionary *item , NSUInteger idx, BOOL *stop) {
    NSLog(@"clientId = %@",  [item objectForKey:@"id"]);
    NSLog(@"clientName = %@",[item objectForKey:@"Q1"]);
    NSLog(@"job = %@",       [item objectForKey:@"Q2"]);
}];

这篇关于Cocoa-Touch - 如何解析本地Json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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