ali
ali

Reputation: 85

JSON parsing giving exception in iPhone app

I am using following code for other JSON data its working but here it gives dictionary exception it has small amount of data.

This is data which i am parsing

   { "affectedDate": 1310515200000, 
   "category": "Sport", 
   "content": "Kl 2100 hver tredje lørdag i måneden arrangerer Harvey's en interaktiv  fotballquiz på våre 13 HD-skjermer. Nivået er høyt, men samtidig er det underholdning i form av videoproduksjoner og midt i blinken for enhver fotballinteressert kristiansander. Quizen er myntet på folk som har lyst å lære litt om fotball - og ikke bare de som vet hvor mange fly som gjennomsnittlig flyr over Goodison Park. Det er 1-4 personer pr lag og gratis påmelding i baren på Harvey's eller på tlf 380 72305. Umbro er hovedsponsor av Harvey's Saturday Night Football Quiz og stiller med flotte rundepremier! \n

\n Vi har også en maratontabell bestående av alle resultatene fra 2011s runder, men hvor vi stryker hvert lag sine fire dårligste poengsummer foran finalen i desember. Laget som vinner sammenlagt sender vi til EM i Polen og Ukraina neste sommer!\n \n Utvalgte deler av tidligere quizer, soundtrack, maratontabell og annen informasjon finner du for øvrig på våre hjemmesider www.harveys.no\n \n Vi garanterer at dette er en månedlig fotballhappening å få med seg!", "eventId": 15946, "image": "http://shelf-media.s3.amazonaws.com/39be3cbc5584eb0e9f61c9926a62d478_gmedium.jpg",

   "latitude": "58.1441382", 
  "longitude": "7.9933589", 
  "title": "HARVEY'S SATURDAY NIGHT FOOTBALL QUIZ" } 



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];
appDelegate.books1 = [[NSMutableArray alloc] init];
appDelegate.dates =[[NSMutableArray alloc]init];
appDelegate.descriptionArray=[[NSMutableArray alloc]init];
NSArray *results = [parser objectWithString:json_string error:nil];
NSLog(@"%@", results);

for (int i=0; i<[results count]; i++) {
  Detail  *aBook = [[Detail alloc] initWithDictionary:[results objectAtIndex:i]];      
  [appDelegate.descriptionArray addObject:aBook];
  [aBook release];  
}

Upvotes: 0

Views: 186

Answers (3)

omz
omz

Reputation: 53551

Your JSON string contains a dictionary as its root object, not an array, so it is initialized as NSDictionary by the objectWithString: method. NSDictionary doesn't have an objectAtIndex: method which is why you see the exception when trying to call it.

Upvotes: 0

Hot Licks
Hot Licks

Reputation: 47699

When I look at the actual web page the outermost structure is an "object" (dictionary). But you parse the input string twice, once expecting a dictionary and the second time an array (it can't be both). It's the array you're accessing when you get the error, I suspect. (Can't tell for sure since you didn't provide any log or other error info.)

In general, you need to test (with isKindOfClass) the type returned from a JSON parser, to make sure it's the type you expect.

Upvotes: 1

lxt
lxt

Reputation: 31294

    Detail  *aBook = [[Detail alloc] initWithDictionary:[results objectAtIndex:i]];

You are making an assumption that all the objects inside your results array are NSDictionary objects - JSON can contain both dictionaries and arrays, so this is far from guaranteed.

Upvotes: 0

Related Questions