如何将解析的JSON数据存储在单个数组中

时间:2011-07-13 06:22:28

标签: iphone objective-c

我已经解析了JSON数据的格式是我的JSON数据 http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder

SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *object = [parser objectWithString:json_string error:nil];
    NSArray *results = [parser objectWithString:json_string error:nil];

2 个答案:

答案 0 :(得分:1)

如何创建数据模式?

@interface book:NSObject {
NSString *catId;
NSString *bookName
}

create properties for these two instance vars.
@end


@implementation book 

@synthesize catId,bookName;

- (id)init {
 self = [super init];
}

- (id)initWithDictionary:(NSDictionary) dict {
 self.catId = [dict valueForKey:@"categoryId"];
  self.bookName = [dict valueForKey:@"name"];
}

- (void)dealloc {
[catId release];
[bookName release];
[super dealloc];
}
@end


and use it like this

 NSMutableArray *bookArray = [[NSmutableArray alloc] initWithCapacity:0];
 NSArray *results = [parser objectWithString:json_string error:nil];
    for (int i=0; i<[results count]; i++) {
   book *bookObject = [[book alloc] initWithDictionary:[results objectAtIndex:i]];
   [bookArray addObject:bookObject];
   [bookObject release];
}

答案 1 :(得分:0)

我认为你可以这样做 将字典添加到数组中。

 for (NSDictionary *dict in mydict) {
            [myArray addObject:dict];
        }

您可以进行更多修改并根据逻辑设置数组中的值 也是关键。 希望这对你有所帮助。我现在还没有检查过。但是可以帮你解决。

干杯.....