gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:22:58 27 4
gpt4 key购买 nike

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

{"quizz":[{"id":"1","Q1":"米奇什么时候出生","R1":"1920","R2":"1965","R3 ":"1923","R4","1234","re​​sponse","1920"},{"id":"1","Q1":"什么时候开始冷战","R1":"1920 ","R2":"1965","R3":"1923","re​​p4","1234","响应","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"]);
}

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

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

最佳答案

JSON 具有严格的键/值表示法,您的 R4 和响应的键/值对不正确。试试这个:

NSString *jsonString = @"{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1965\",\"R3\":\"1923\",\"R4\":\"1234\",\"response\":\"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"}]}


我用这段代码测试过:

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

我的印象是你复制了旧代码,因为你没有使用苹果的序列化和枚举器而不是 Fast Enumeration .整个枚举的东西可以简单地写成

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

甚至更喜欢 block based 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"]);
}];

关于objective-c - Cocoa-Touch - 如何解析本地 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12150595/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com