I tried to parse this:
{"event":[{"event_id":"9","title":"event 10","welcome_logo":"20140715130727_252.png"}],"succeed":1}
This is my code:
NSString *strURL=[NSString stringWithFormat:#"http://loc**host/summit/event_login.php"];
NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult=[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSDictionary *element=[jsonArray objectAtIndex:0];
NSString *title = [element objectForKey:#"title"];
NSString *image = [element objectForKey:#"welcome_logo"];
But this guy came and disturbs me:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x9e7d010'
So I'm wondering what I did I do wrong. I tried to google it and many said that I assume to use an array when the data is actually a dictionary. Is that true? So what do I need to do?
Change
NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSDictionary *element=[jsonArray objectAtIndex:0];
to
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSArray *events = [jsonDict objectForKey:#"event"];
if ([events count] > 0) {
NSDictionary *element=[events objectAtIndex:0];
}
because your json data is in the form of dictionary. And event holds the list (array) of events. And do a count check before accessing objectAtIndex: from events.
Related
This is the following code I have
id dataDict = [NSPropertyListSerialization
propertyListWithData:[dictionaryStr dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
format:NULL
error:NULL];
if ([dataDict isKindOfClass:[NSDictionary class]]) {
for (NSString *key in dataDict) {
// value string crashes with the following exception -[NSTaggedPointerString objectForKey:]:
NSString *value = [dataDict objectForKey:key];
}
}
There is a user who is having a crash occur on the following line NSString *value = [dataDict objectForKey:key]; according to crashlytics. This doesn't make any sense to me because I check beforehand if dataDict belongs to the NSDictionary class. Can someone please explain to me how I can prevent this crash?
I'm trying to parse a JSON file so I can use it's contents as NSStrings and display in UILabels throughout my application.
My JSON:
{
"CustomText": [
{
"bodyText": "This is the body text",
"loginText": "This is the loginText",
"extraText": "This is the extra text"
}
]
}
My code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"File" ofType:#"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
NSArray *customStrings = json[#"CustomText"];
NSString *body = [customStrings valueForKey:#"bodyText"];
NSString *login = [customStrings valueForKey:#"loginText"];
self.labelText.text = body;
I'm not sure why it breaks, I have looks and surprisingly didn't find much on how to best use a local JSON file. Any help would be great.
Error message:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI length]:
unrecognized selector sent to instance 0x7f8080781740'
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"File" ofType:#"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
NSArray *customStrings = json[#"CustomText"];
NSString *body = [[customStrings objectAtIndex:0] valueForKey:#"bodyText"];
NSString *login = [[customStrings objectAtIndex:0] valueForKey:#"loginText"];
self.labelText.text = body;
Use keypath
NSString * body = [json valueForKeyPath:#"CustomText.bodyText"];
self.labelText.text = body;
So I have the photograph below which is a screenshot I took of a JSON script I am trying to read from a URL. In the code section below, I managed to get the 'name' attribute and assign it to the 'name' NSString object, but when I try do the same for 'address' I get the following error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980'
whats the difference between how I did it inside 'venues' than inside 'location'? I tried many different ways to do it, some including arrays, but nothing seems to work.
NSURL *url = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *responseDictionary = [dataDictionary objectForKey:#"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:#"venues"];
for (NSDictionary *dict in venueDictionary) {
NSLog(#"%#", dict);
NSString *name = [dict objectForKey:#"name"];
NSDictionary *locationDictionary = [dict objectForKey:#"location"];
for (NSDictionary *locationDict in locationDictionary) {
NSString *address = [locationDict objectForKey:#"address"];
NSLog(#"%#", address);
}
}
You're expecting the "location" key to point to an array, but it doesn't. It points to an object, get rid of for (NSDictionary *locationDict in locationDictionary) { and just pull the address out of locationDictionary instead.
1.I get JSON data from web services and add It's to NSDictionary
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
2.Then I Show "dic" in NSLog It's present
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
3.Then I get data value from "dic" in to new NSDictionary "dt"
NSDictionary *dt = [dic objectForKey: #"query"];
4.Then I Show "dt" in NSLog It's present
({ID = 01;IndexNo = 1;Picture = "img/food-48.png";})
5.I want to get "ID" from "dt". I use this code
NSString *ID = [dt objectForKey: #"ID"];
but it's error
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a40a60
Can you try this
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *arr = [dic objectForKey: #"query"];
NSDictionary *dt = arr[0];
NSString *ID = [dt objectForKey: #"ID"];
[dic objectForKey: #"query"] returns an NSArray actually. Note these square brackets in the output from the first NSLog statement:
{"query": [...]}
Try this:
NSArray *dtArray = [dic objectForKey: #"query"];
NSDictionary *dt = dtArray[0];
NSString *ID = [dt objectForKey:#"ID"];
Or to use the key-value coding behavior of NSDictionary and NSArray:
NSString *ID = [[dic valueForKeyPath:#"query.ID"] firstObject];
NSDictionary *dt = [[dic objectForKey: #"query"] objectAtIndex:0];
NSString *ID = [dt objectForKey: #"ID"];
You are trying to get value for key from array. change these lines.
From your jsonStructure
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
object for key "query" is an array as {} is a dictionary and [] is an array. so do the following
NSArray *tempArray = [dt objectForKey:#"query"];
NSDictionary *tempDictionary = [tempArray objectAtIndex:0];
NSString *ID = [tempDictionary objectForKey:#"ID"];
Hope this helps.
I have some problem in my project. So i used JSON for take some information.
When i have JSON with folders(example: i have NSDictionary with name Playlist in this playlist i have NSString name and album) for this i make this code:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSDictionary *playlist =[allDataDictionary objectForKey:#"playlist"];
for (NSDictionary *diction in playlist) {
NSDictionary *artist = [diction objectForKey:#"artist"];
NSDictionary *song = [diction objectForKey:#"song"];
NSString *name = [artist objectForKey:#"name"];
NSString *namesong = [song objectForKey:#"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
It's work perfect! BUT! When i don't have any folders, only, JSON without NSDictionary only NSStrings, how make? Sorry for my stupid question but really i tried write:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSString *name = [allDataDictionary objectForKey:#"name"];
NSString *namesong = [allDataDictionary objectForKey:#"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
But i have error, also my app crashed:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7615c90'
So, what i doing wrong?
The error says you have an NSArray, not an NSDictionary. This corresponds to a JSON array:
[
{ "name" : "fred" },
{ "name" : "jane" }
]
Here you have an array of dictionaries. You may want:
NSArray *people = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
for (NSDictionary *person in people) {
NSLog(#"name is %#", person[#"name"];
}