Reputation: 411
Could you please forgive me for eventual mistakes i can make asking this question me it's my first one here.
After reading several topics on this website, like this one first i'll try to use the describe methods but it still doesn't work @ all :-(
My .json file looks like this
{ "speakers" :
[
{
"name":"Value",
"picture": "URL VALUE",
"business":"VALUE",
"desc":"VALUE",
"twitter": "URL VALUE"
}
{
...
}
]
}
So this is my reasoning :
I firstly have a dictionary which contains speaker attribute
This one contains an array, field by some dictionnaries within "name", "business",... attr.
So, this is my obj-C code :
NSString *URLStr = @"URLofMyJsonFile";
NSURLRequest *JSONRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:URLStr ]]];
NSData *JSONData = [NSURLConnection sendSynchronousRequest:JSONRequest returningResponse:nil error:nil];
NSError *parsingError = nil;
NSDictionary *speakerDictionnary = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:&parsingError];
NSArray *speakersArray = [speakerDictionnary objectForKey:@"news"];
for (NSDictionary *oneSpeaker in speakersArray) {
NSLog(@"The speakers's name is %@", [oneSpeaker objectForKey:@"name"]);
NSLog(@"The speakers's business is %@", [oneSpeaker objectForKey:@"business"]);
NSLog(@"The speakers's desc is %@", [oneSpeaker objectForKey:@"desc"]);
}
EDIT : I remplace the right URL of my Script with Dummy
Upvotes: 2
Views: 275
Reputation: 1652
As omz mentioned the json is wrong. you can try below code :
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.appios.fr/client/takeoff/app/script/jsonSpeaker.json"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *reponse,NSData *data,NSError *error){
if (!error) {
NSError *jsonError;
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
NSArray *speakersList = [json objectForKey:@"speakers"];
[speakersList enumerateObjectsUsingBlock:^(NSDictionary *dict,NSUInteger idx,BOOL *Stop){
NSLog(@"Name : %@",[dict objectForKey:@"name"]);
}];
}
} ];
Upvotes: 0
Reputation: 53551
Your JSON isn't valid, there needs to be a comma between the individual speaker dictionaries.
{ "speakers" :
[
{
"name":"Value",
"picture": "URL VALUE",
"business":"VALUE",
"desc":"VALUE",
"twitter": "URL VALUE"
} <=== MISSING COMMA HERE
{
...
}
]
}
Upvotes: 3