解析json数据给出错误

时间:2011-08-17 06:02:13

标签: iphone objective-c json

我从这个链接中获取JSON数据。

http://www.krsconnect.no/community/api.html?method=event&appid=620&eventid=15946&affecteddate=1310515200000

我想在aDetail类的Detail对象中存储所有必需的元素,如图像媒体标题等,但是它给出了错误。

这是我的代码:

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=event&appid=620&eventid=15946&affecteddate=1310515200000"]];


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];


for (int i=0; i<[results count]; i++) {

    NSDictionary*dictOne=[results objectAtIndex:i];
    Detail  *aDetail = [[Detail alloc] initWithDictionary:[results objectAtIndex:i]];       

    [appDelegate.descriptionArray addObject:aDetail];

     }

2 个答案:

答案 0 :(得分:1)

这不是你如何使用SBJSon。试试这个:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=event&appid=620&eventid=15946&affecteddate=1310515200000"]];


    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *object = [json_string JSONValue];

答案 1 :(得分:0)

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSError *error;

SBJSON *json = [[SBJSON new] autorelease];

id obj = [json objectWithString:json_string error:&error];

if ([obj isKindOfClass:[NSDictionary class]]) {

           [appDelegate.descriptionArray addObject:obj];

}else if ([obj isKindOfClass:[NSArray class]]) {

         for (NSDictionary *aDetail in obj) {

              [appDelegate.descriptionArray addObject:aDetail];

         }
}