I'm attempting to read a JSON array from my php generated file at www.tiritium.com/standings.php and it works just fine on Android but I cannot get it to read out of the file. It opens it properly but I can't get get the NSDictionary into a NSArray.
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestNames = [json objectForKey:#"name"]; //2
NSLog(#"name: %#", latestNames); //3
I've narrowed down the error to //2 but I can't seem to figure out what is going on. I'm not sure if it is with how my data is being output from the script or with my code in the app itself.
2013-08-14 16:39:51.992 Derby Days[7951:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0
2013-08-14 16:39:51.993 Derby Days[7951:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0'
Is the error message I am receiving.
Read the exception
-[__NSCFArray objectForKey:]: unrecognized selector
The root of your JSON is an array, not a dictionary.
Your JSON file represents an array, not a dictionary. If you need to get an array of names out of it, you use key value coding like NSArray *names = [json valueForKey:#"name"];. This will give you an array of NSStrings.
Related
I'm a little new to all this and I'm having trouble getting information out of an incoming JSON NSDistionary. Heres the dictionary:
As a first step I'm trying to retrieve the address sections postcode, here's the code:
for (NSString *address in jsonData [#"address"][#"postcode"]) {
NSLog(#"address is ===> %#", address);
}
However when I run this i am receiving an NSException error:
'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x79e4daf0'
Any help with this would be very much appreciated.
Thanks
As Shown your JSON file contain an array of 3 objects, we first convert data to array.
Then you have to access dictionary by using
1.valueForKeyPath#"dictionarykey1.dictionarykey2".
2.objectForKey#"dictionarykey1".
NSLog(#"JSON: %#", responseObject);
NSArray *Response=(NSArray*)[responseObject copy];
NSString * Address=[Response[0] valueForKeyPath:#"address.postcode"];
I have a json string that I have received form some API, which looks like this:
[{"id": 2, "title": "Hello world!", "source": "htp://abc.com/hello_world", "blog": "abc.com", "rating": 0, "date": "2014-08-16T15:44:29Z", "tags": ["programming"]}, {"id": 1, "title": "Why we do not fly to space?", "source": "htp://habrahabr.ru/post/233119/", "blog": "habrahabr.ru", "rating": 5, "date": "2014-08-13T15:56:34Z", "tags": ["space", "science", "future"]}]
I receive it like this:
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
NSArray *jsonArray = (NSArray*)JSONDictionary;
for (int i=0; i<=jsonArray.count-1; i++) {
NSDictionary *item = [jsonArray objectAtIndex:i];
. . .
NSString* tags = [item objectForKey:#"tags"];
. . .
}
So now I have this NSArray. I can extract everything except tags.
I have tried:
NSLog(#"Tags: %#", tags);
NSArray *json = [NSJSONSerialization
JSONObjectWithData:[tags dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
error:&error];
This gives me exception:
-[__NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance 0x109399810
2014-08-17 14:45:48.224 ReadLater[38625:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance
Then, I have tried this:
NSArray *strings = [tags componentsSeparatedByString:#","];
This gives me exception:
-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1096a6680
2014-08-17 14:50:57.676 ReadLater[38758:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1096a6680'
When I print out tags variable, it looks like this:
Tags: (
space,
science,
future
)
This is everything that I could possibly tell you about this problem. Hope this gives you idea of whats wrong, coz I suspect that I simply don't know something about this particular case.
Thank you very much.
Tags is not a string, it is an array. So, to properly get the tags, use this
NSArray *tags = [item objectForKey:#"tags"];
To get the tag from the first dictionary in your JSON, you would do this
NSString *firstTag = tags[0]; //this would be "programming"
I am trying to determine what is causing this error:
2014-06-08 20:40:44.076 Database[8656:70b] -[__NSArrayI length]:
unrecognized selector sent to instance 0x8a44050
Here is the code.
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
This is the breaking point where the error occurs: NSLog([json allKeys]);
SAMPLE OF THE JSON BEING USED AS INPUT:
{"1":{"key":"1","contentONE":"aaa","contentTWO":"testing"},"2":{"key":"2","contentONE":"bbb","contentTWO":null},"3":{"key":"3","contentONE":"ccc","contentTWO":"testing"}}
[json allKeys] returns an NSArray, while NSLog expects a formatting string. Try this:
NSLog(#"%#", [json allKeys]);
I basically get this error
'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x581f0'
on my program. I think it refers to this call I make,
if (data != nil) {
if([data objectForKey:#"username"]){
// NSArray *check= [[NSArray alloc]init];
//check=[data allValues];
[dict setObject:[data allValues] forKey:#"args"];
}else{
[dict setObject:[NSArray arrayWithObject:data] forKey:#"args"];
}
at the setObject:[data allValues]. I don't know why it gives that error but data is an NSDictionary and I'm getting all the values and placing it in an array.
Is the error happening here:
if([data objectForKey:#"username"]){
I assume so, as that is the only place objectForKey seems to be called. You are calling it on a variable called 'data', which i'm guessing simply is not a dictionary. You should NSLog its type to see.
We have the following method where we are trying to access an array object at a given index. The array is resultArr. When we do a resultArr count it gives us a result of 13. So we know that the array is not null but when we try to do objectAtIndex it crashes with the error.
Function:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray *keys = [json allKeys];
NSLog(#"keys: %#",keys);
NSArray* htmlAttributions = [json objectForKey:#"html_attributions"]; //2
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
NSArray* statusArr = [json objectForKey:#"status"]; //2
NSLog(#"htmlAttributions: %#",htmlAttributions);
NSLog(#"result: %#", resultArr); //3
NSLog(#"status: %#", statusArr); //3
NSLog(#"resultCount: %d",[resultArr count]);
[resultArr objectAtIndex:0];
}
Error:
2012-04-01 22:31:52.757 jsonParsing[5020:f803] resultCount: 13 2012-04-01 22:31:52.759 jsonParsing[5020:f803] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900 2012-04-01 22:31:52.760 jsonParsing[5020:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900'
*** First throw call stack:
Thank you.
The error message is fairly descriptive. One of the objects that your code expects to be an NSArray is actually an NSDictionary. You cannot access fields inside of an NSDictionary by using NSArray methods (and casting from NSDictionary* to NSArray* will not convert an NSDictionary into an NSArray).
This would mean that inside of the JSON, one of your elements was serialized as an object/associative array instead of as a plain array. You can easily determine which one by looking at your JSON data as text, and finding the item that uses { and } instead of [ and ].
You are saying
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
But that does not make this object ([json objectForKey:#"result"]) an NSArray. It is an NSDictionary, and sending it a message that NSDictionary does not respond to (objectAtIndex:) causes a crash.
You were able to send it the count message without crashing because NSDictionary does happen to respond to the count message. But your preconception that this is an array is still mistaken.
You cannot cast an NSDictionary* to an NSArray* as you tried to do with this line: NSArray* resultArr = (NSArray *)[json objectForKey:#"result"];, then call -objectAtIndex.