Xcode 如何解析 Json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10460839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode how to parse Json Objects
提问by HernandoZ
I manage to get Json for my server and parse in Xcode , I can then convert Json to a object and loaded my uitableview, the problems is my Json have 7 objects ..
我设法为我的服务器获取 Json 并在 Xcode 中解析,然后我可以将 Json 转换为对象并加载我的 uitableview,问题是我的 Json 有 7 个对象..
you can see the full Json here
2012-05-05 10:45:02.727 passingdata[63856:fb03] array : {
posts = {
Friday = (
{
GymClass = {
"CLASS-TYPE2" = "";
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Friday;
TIME = "13:00 - 14:30";
};
}
);
Monday = (
{
GymClass = {
"CLASS-TYPE2" = "Fighters Training";
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Muay Thai ";
"DAY_OF_WEEK" = Monday;
TIME = "09:30 - 11:00";
};
}, ......
with this code I can get friday's "Friday" and display the GymClass info "GymClass" on my table
使用此代码,我可以获得星期五的“星期五”并在我的桌子上显示 GymClass 信息“GymClass”
- (void)fetchedData:(NSData *)responseData { //parse out the json data
searchResults2 = [NSMutableArray arrayWithCapacity:10];
NSError* error;
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"array : %@",dictionary);
NSArray *array = [[dictionary objectForKey:@"posts"] objectForKey:@"Friday"]; // retrieve that Day Gym Classes
if (array == nil) {
NSLog(@"Expected 'posts' array");
return;
}
for (NSDictionary *resultDict in array)
{
SearchResult *searchResult3;
searchResult3 = [self parseTrack:resultDict];
if (searchResult3 != nil) {
[searchResults2 addObject:searchResult3];
}
}
[self.tableView reloadData];
}
- (SearchResult *)parseTrack:(NSDictionary *)dictionary
{
SearchResult *searchResult1 = [[SearchResult alloc] init];
searchResult1.classType= [[dictionary objectForKey:@"GymClass"] objectForKey:@"CLASS_TYPE"];
searchResult1.classLevel= [[dictionary objectForKey:@"GymClass"] objectForKey:@"CLASS_LEVEL"];
NSLog(@"parse track = %@", searchResult1);
return searchResult1;
}
I can get the elements for one day but how do I get the Elements for every day (Mon,Tue...Sun)so i can display on my table by sections?
我可以获取一天的元素,但如何获取每天(周一、周二...周日)的元素,以便我可以按部分显示在我的桌子上?
thanks for your help..
感谢您的帮助..
回答by Jonathan Naguin
Well in your code, you already are doing that:
那么在您的代码中,您已经在这样做了:
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"array : %@",dictionary);
NSArray *array = [[dictionary objectForKey:@"posts"] objectForKey:@"Friday"]; // retrieve that Day Gym Classes
Wen can start from there to retrieve only the object posts
:
Wen 可以从那里开始只检索对象posts
:
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSDictionary *posts = [dictionary objectForKey:@"posts"];
//Iterate over posts
for (NSArray *aDay in posts){
//Do something
NSLog(@"Array: %@", aDay);
}
Here, i am using Fast Enumeration to iterate over the dictionary, you should check this here.
在这里,我使用快速枚举来迭代字典,你应该在这里检查。