开发者

JSON parsing giving exception in iphone app

The JSON Data format is given bewlow

{ "affectedDate": 1310515200000, "category": "Kultur", "content": "I en festning fra 1600-tallet, ved hjelp av lys, lyd og avansert teknologi møtes de besøkende av synspunkter som kan virker fremmede, de får høre sterke, personlige historier og et mangfold av meninger og stemmer.\n
\n
Vi spør blant annet:\n
Har Sørlandskristendommen ført til maktmisbruk og dobbelmoral? \n
Må barna oppdras i Guds navn for ikke å mangle viktige verdier? og \n
Hvorfor er det bare få som tør å si høyt at de kommuniserer med de døde?\n
 \n
â€Himmelen over Sørlandet†er Vest-Agder-museets største utstillingssatsing noensinne, og  mer enn 130 mennesker har bidratt.\n
\n
 Velkommen til en tankevekkende utstilling!", "eventId": 14569, "imageId": 14597, "large":  "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_large.jpg", "main": true, "medium": "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_gmedium.jpg", "mini": "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_p开发者_如何学运维mini.jpg", "small": "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_pmedium.jpg", "thumbWide": "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_pwide.jpg", "thumbnail": "http://shelf-media.s3.amazonaws.com/7c3f11582bce0b1e153619e436a622da_gsmall.jpg" "title": "Himmelen over Sørlandet" }





SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/description.html"]];
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];
appDelegate.books1 = [[NSMutableArray alloc] init];
appDelegate.dates =[[NSMutableArray alloc]init];


for (int i=0; i<[results count]; i++) {
    NSDictionary *dictOne = [results objectAtIndex:i];


    //  NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]);
    //  NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
    //  NSLog(@"%@ - %@", [eventDict objectForKey:@"location"]);


    NSLog(@"%@ - %@", [dictOne objectForKey:@"affectedDate"]);

    NSString*affectedDate=[dictOne objectForKey:@"affectedDate"];
    NSString *category=[dictOne objectForKey:@"category"];






//  Detail *aBook=[[Detail alloc] initWithDate:affectedDate category:];



}


This might prove helpful. It's basically a JSON validator to check if your JSON files/code is valid. You might want to give it a go.

http://jsonlint.com/

Edit: I saw your comment. One bug - after thumbnail in your JSON string you have forgotten to put a comma. The other issue is that your content seems to have these special characters that aren't part of the traditional ascii set. I'm not sure on how to handle that


If this is the error you are getting

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6e07dd0'

Then you are trying to access an object using objectAtIndex: for an NSDictionary object instead of a NSArray. NSDictionary does not support objectAtIndex: Please check your response after parsing and then check the type.


If you added JSON as library, Then you need to add -all_load to the linker flags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜