Objective-C JSON parsing error - iphone

I'm trying to parse this JSON Array:
http://www.ifanatic.hu/api/get_recent_posts/?dev=1
But I get this error:
2012-07-15 22:31:59.038 JSONTest[610:f803] -[__NSCFDictionary indexOfObject:]: unrecognized selector sent to instance 0x6a92850
2012-07-15 22:31:59.040 JSONTest[610:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary indexOfObject:]: unrecognized selector sent to instance 0x6a92850'
*** First throw call stack:
(0x13d7052 0x1568d0a 0x13d8ced 0x133df00 0x133dce2 0x236b 0xa23a59 0xa21e94 0xa22eb7 0xa21e4f 0xa21fd5 0x966f6a 0x3989bbd 0x3a565ea 0x3980298 0x3a5616b 0x3980137 0x13ab97f 0x130eb73 0x130e454 0x130ddb4 0x130dccb 0x12c0879 0x12c093e 0x2ea9b 0x1cb8 0x1c15)
terminate called throwing an exception
Here is my source:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSArray* latestPosts = [responseString JSONValue];
NSDictionary* post = [latestPosts objectAtIndex:[latestPosts indexOfObject:0]];
NSString* title = [post objectForKey:#"title"];
label.text = [NSString stringWithFormat:#"Title: %#", title];

That JSON object's root element is translated to an NSDictionary when parsing the JSON. And NSDictionary doesn't respond to the
indexOfObject:
message. What you need to do is to use:
NSDictionary *post = [[latestPosts objectForKey:#"posts"] objectAtIndex:0];
etc.

Related

exception 'NSInvalidArgumentException':-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x752c670 [duplicate]

This question already has an answer here:
SBJson Execptions after parsing (__NSArrayM objectForKey:)
(1 answer)
Closed 9 years ago.
I use sbjson to parse json,
this is the Json I'm trying to parse :
{"R":"0","S":"","T":"","M":[{"C00":"2013-08-16 17:35:03"}]}
and this is my code:
NSString *response = [self post:#"9903" content:payload state:#""];
NSDictionary *dict = [response JSONValue];
NSString *result = [dict objectForKey:#"R"];
NSLog(#"result=%#",result);
if ([#"0" isEqualToString:result]) {
NSDictionary *msg = [dict objectForKey:#"M"];
NSString *C00 = [msg objectForKey:#"C00"];//here the exception Statement
NSString *tokenString = [NSString stringWithFormat:#"%#",C00];
NSLog(#"tokenString%#",tokenString);
return tokenString;
}else {
return nil;
}
the exception log:
2013-08-16 17:45:44.902 VEP[4731:c07] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7250300
2013-08-16 17:45:44.903 VEP[4731:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7250300'
what's wrong? thanks for your help!
Because your key 'M:' contain array of dictionary not only dictionary. So, write it as a,
NSDictionary *msg = [[dict objectForKey:#"M"] objectAtIndex:0];

JSONKit invalid arguement when try to copy

I am parsing JSON data with JSONKit as NSMutableDictionary.
NSString *str = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *jsonResponse = [self.responseData objectFromJSONData];
NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
[newData addEntriesFromDictionary:[jsonResponse mutableCopy]];
When i do this i am getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableDictionary addEntriesFromDictionary:]: dictionary argument is not an NSDictionary'
I am trying to figure out what is causing this problem. I know that jsonResponse is an object of JKArray from my other experience.
I need help.
Thanks.
Try the following:
id object = [self.responseData objectFromJSONData];
NSLog(#"%#", [object class]);
Most likely your response is an array instead of a dictionary.
If you really want to convert the array into a dictionary, you could do something like this, using a self-defined key:
NSArray *array = [self.responseData objectFromJSONData];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:array forKey:#"posts"];
Though perhaps there are some better options if you could show me the contents of your array.

problem in parsing JSON response in iphone

i am getting json data from this link
now i want data from "html_instructions": part
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
NSString *string;
for(NSDictionary *di in resultarr){
NSLog(#"for loop");
string = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
but after printing "for loop" in console it is throwing an exception
2011-05-20 16:16:26.997 speedymap[759:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60
2011-05-20 16:16:26.999 speedymap[759:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60'
please help me
i want html_instructiuons field value
Check if resultarr and di really contains anything or not.
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
CFShow(resultarr);
NSString *string;
for(NSDictionary *di in resultarr){
CFShow(di);
NSLog(#"for loop");
NSArray *tempArr = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
http://jsonviewer.stack.hu/#http://maps.googleapis.com/maps/api/directions/json?origin=delhi&destination=noida&waypoints=&sensor=true
Check the viewer and set the values accordingly. [di objectForKey:#"legs"] returns an array. the first object of that array is a dictionary which has the key steps. But that too returns another array.

Searching an NSMutableArray with a String and returning the entire compared Array

it seems my Problem isn't a problem for anybody eles because I havend fount anything about it.
so it maybe isn't such a big Problem but for me it is.
I have this MutableArray filled with alot of data from an XML file.
-Name -Age -Address
The search goes for the Name, and the filtering works pretty fine so far.
what I do is search the array with rangeOfString but that only returns the String (-Name) and not the Array with it's content like the original Array because its only a string now.
can anyone tell me how do I accomplish this
That's my search so far
if ([[self searcher] length] != 0)
{
for (NSString *currentString in [self listOfContent])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self filteredListOfContent] addObject:currentString];
}
searcher is the String in the SearchBar.
or is there any other more efficent way or is it possible to search for any value in the MutipleArry?!?
Any Ideas and suggestions are welcome
I changed the code to this
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfContent)
{
//NSArray *array = [dictionary objectForKey:LNAME];
[searchArray addObject:dictionary];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"array %#", searchArray);
if ([sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)
[filteredListOfContent addObject:searchArray];
}
the log shows that the filter seems to work but then I get this error
2010-10-22 16:18:09.708 TableView[6114:207] -[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540
2010-10-22 16:18:09.712 TableView[6114:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540'
can anyone tell me what the problem is
And Still no solution found I changed the Code to this:
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in contentsList)
{
NSArray *array = [dictionary allValues];
[searchArray addObjectsFromArray:array];
}
for (NSDictionary *dict in searchArray)
{
if ([[dict valueForKey:#"NAME"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(#"Filter %#", dict);
[searchResults addObject:dict];
}
now i Have the array with the values but still get the error
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
can anyone explain me waht taht that error means or waht I did wrong?!?
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
That error means you treated an NSString as if it were an NSDictionary by sending the message -objectForKey: to it.

NSCFArray length]: error, array regex

StringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
//Regex Out Artist Name
//NSString *regEx = ;
NSArray *iTunesAristName = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([iTunesAristName isEqual:#""]) {
NSLog(#"Something has messed up");
//Regex Out Song Name
}else{
NSLog(iTunesAristName);
}
NSLog(iTunesAristName);
[stringReply release];
I just keep getting this error ?
2010-09-29 21:15:16.406 [2073:207] *** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800
2010-09-29 21:15:16.406 [2073:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800'
2010-09-29 21:15:16.407 [2073:207] Stack: (
please help its driving me crazy
The first argument to NSLog is supposed to be a format string. You're passing an NSArray. When the function tries to treat your array as a string, you get that error. Instead, use NSLog(#"%#", iTunesAristName);.
Chuck has answered your question, but I've noticed something else that is problematic.
NSArray is an array, not a string, so [iTunesArtistName isEqual:#""] will never return true, because they are different classes. Even if iTunesArtistName was a string, it should be compared using the isEqualToString: method, not isEqual:.
If you want to extract only the artist's name, you might be able to do this:
NSArray *matches = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([matches count] == 0)
{
NSLog(#"Could not extract the artist name");
}
else
{
NSString *iTunesArtistName = [matches objectAtIndex:0];
NSLog(#"Artist name: %#", iTunesArtistName);
}
I see you're using RegexKitLite, make sure you import libicucore.dylib, i was getting the same error until i imported that library.