Xcode如何解析Json Objects [英] Xcode how to parse Json Objects
问题描述
我设法为我的服务器获取Json并在Xcode中解析,然后我可以将Json转换为对象并加载我的uitableview,问题是我的Json有7个对象..
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
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";
};
}, ......
使用此代码我可以获得星期五的星期五并显示GymClass我桌上的信息GymClass
with this code I can get friday's "Friday" and display the GymClass info "GymClass" on my table
- (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?
感谢您的帮助..
推荐答案
在您的代码中,您已经这样做了:
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可以从那里开始只检索对象 posts
:
Wen can start from there to retrieve only the object 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.
这篇关于Xcode如何解析Json Objects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!