我正在开发iphone应用程序。在这里,我正在阅读一个JSON数组,我已经将其值以数据形式获取到我得到的值,格式如下:
Array : {
global = {
players = {
1 = "John Doe, A school";
10 = "Jonathan Doe, Another school";
2 = "Joe Doe, Another school";
3 = "Jane Doe, A school";
4 = "Jay Doe, Another school";
5 = "Jimmy Doe, A school";
6 = "Jeremy Doe, Another school";
7 = "Johnny Doe, A school";
8 = "Jeremiah Doe, Another school";
9 = "Jennifer Doe, A school";
};
schools = {
1 = "A school";
2 = "Another school";
3 = "The school";
4 = "The other school";
5 = "That school";
};
text = "A dynamic text that needs to be displayed to the player.";
};
现在我的问题是我如何阅读全球的每个部分?看完之后我怎么读学校?等...
答案 0 :(得分:2)
看看这个问题Comparison of JSON Parser for Objective-C (JSON Framework, YAJL, TouchJSON, etc)
它有一个几乎所有主要JSON解析器的列表。 JSON解析器会将JSON转换为数组/字典,并允许您使用objectForKey和objectAtIndex方法来访问结构。
答案 1 :(得分:1)
使用JSONKit。它是更新更好的SBJson版本
答案 2 :(得分:0)
使用TouchJSON。它非常易于使用,并提供全面的文档。
答案 3 :(得分:0)
就这样做
for (var i in global ) {
for(var j in global [i].schools ){
alert(global [i].schools[j]);
}
}
答案 4 :(得分:0)
SBJson是另一个很好的Objective-C JSON库。它会将JSON解析为嵌套的NSArray和NSDictionary对象。规范用法如下:
#import "SBJson.h"
NSString *jsonString = @"{
'name': 'Simon',
'address': {'street': '1 High Street', 'town': 'Anytown'}
}";
NSDictionary *jsonData = [jsonString JSONValue];
NSLog(@"Simon lives in %@", [jsonData valueForKeyPath:@"address.town"]);
输出:
Simon lives in Anytown
顺便说一下,如果你想要一个有序数据列表(例如你的例子中的players
),而不是使用字典(用大括号和键:值数据表示),你可以只使用一个列表(方括号括起一系列值)。所以你的数据变成了:
data: {
global: {
players: [
"John Doe, A school";
"Joe Doe, Another school";
"Jane Doe, A school";
"Jay Doe, Another school";
"Jimmy Doe, A school";
"Jeremy Doe, Another school";
"Johnny Doe, A school";
"Jeremiah Doe, Another school";
"Jennifer Doe, A school";
"Jonathan Doe, Another school";
]
schools: [
"A school";
"Another school";
"The school";
"The other school";
"That school";
],
text: "A dynamic text that needs to be displayed to the player.";
}
};