Objective-C Json Fetch And Parse Data - ios

I'm trying to parse data from my api.My code is correct.I used it a lot of time.But I didn't get any error like this.What I am doing wrong.Can you help me ?
-(void)getStatu {
NSURL *urlPath = [NSURL URLWithString:#"http://myurl.com/m/cc/cc_today.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:urlPath];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"%#",dataDictionary);
_ccStatus = [NSMutableArray array];
NSArray *statuArray = [dataDictionary objectEnumerator];
for (NSDictionary *statuDictionary in statuArray) {
CCStatu *status = [CCStatu statuWithTitle:[statuDictionary objectForKey:#"gelen"]];
status.cevap = [statuDictionary objectForKey:#"cevap"];
status.cort = [statuDictionary valueForKeyPath:#"cort"];
status.kayip = [statuDictionary valueForKeyPath:#"kayip"];
status.lort = [statuDictionary valueForKeyPath:#"lort"];
status.tekil = [statuDictionary valueForKey:#"tekil"];
[_ccStatus addObject:status];
}
}
This is my json
2015-08-13 23:54:41.362 CSGB[3215:209292] {
cevap = "37,627";
cort = "00:00:48";
gelen = "54,247";
kayip = "16,620";
lort = "00:01:17";
sl = "46.30 %";
tekil = "3,316";
}
And this is error
2015-08-13 23:54:58.330 APP[3215:209292] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30
2015-08-13 23:54:58.336 APP[3215:209292] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30'

Your code is iterating the data incorrectly. You only have a single dictionary. There's nothing to iterate.
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"%#",dataDictionary);
_ccStatus = [NSMutableArray array];
CCStatus *status = [CCStatu statuWithTitle:dataDictionary[#"gelen"]];
status.cevap = dataDictionary[#"cevap"];
status.cort = dataDictionary[#"cort"];
status.kayip = dataDictionary[#"kayip"];
status.lort = dataDictionary[#"lort"];
status.tekil = dataDictionary[#"tekil"];
[_ccStatus addObject:status];

Related

'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000617461644'

I am developing simple app that gets json data and stores into different variables
here is my code that gets json data
if ([method isEqualToString:#"getmobiledata"]){
defaultDict = [[NSMutableDictionary alloc]init];
[defaultDict addEntriesFromDictionary:[NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]];
mobile = [defaultDict objectForKey:#"data"];
}
here is my son data:
{
data = (
{
id = 1;
package = 819MB;
rate = "$1";
type = Somnet;
},
{
id = 2;
package = "1,638MB";
rate = "$2";
type = Somnet;
},
);
}
here is paring json into nstring :
for(NSDictionary *getData in mobile){
NSString *idno = [getData objectForKey : #"id"];
NSString *package = [getData objectForKey :#"package"];
NSString *rate = [getData objectForKey :#"rate"];
NSString *type = [getData objectForKey :#"type"];
}
please help me how to solve this
The data model will collide with the same name as the system property ID, and it will collide and crash.
You can use a new ID like this:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
if([key isEqualToString:#"id"])
self.NewId = value;
}

Assigning text to UILabel gives error after parsing JSON

I've the following code for receiving response from PHP web service in JSON Format:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
NSLog(#"Response from Server : %#", responseStringWithEncoded);
[self getData:responseStringWithEncoded];
}
-(void) getData:(NSString *) responseStringWithEncoded{
NSData *data = [responseStringWithEncoded dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"object for key ime = %#", [json objectForKey:#"imei"]);
NSString * jimei = [json objectForKey:#"imei"];
NSLog(#"jimei = %#", jimei);
// NSDictionary * jimei = [json objectForKey:#"imei"];
imeiLable.text = jimei;
}
I am successfully retrieving data in simulator but when assigning one value among received string(NSDictionary) to imeiLable.text it gives following error.
Here is the output:
Request data = { URL: http://localhost/getjsonimei.php?imei=478593219801234 }
Response from Server : {"id":7,"imei":478593219801234,"mname":"Samsung Glaxy","pamount":"2000 rupees","pname":"Faizi","address":"House number 88, block 31","cnumber":11122233,"nic":"87456893"}
object for key ime = 478593219801234
jimei = 478593219801234
Here is detailed description of simulator resulting string(dictionary) and error.
2017-05-24 20:01:13.229 imiechecker[4005:61372] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb01b3472adbb0923
2017-05-24 20:01:13.263 imiechecker[4005:61372] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb01b3472adbb0923'
I also tried following approach:
NSDictionary * jimei = [json objectForKey:#"imei"];
but having the same error.
Please suggest where I am doing it wrong?
Your IMEI number is of data type NSNumber, not NSString.
Try this:
NSString * jimei = [NSString stringWithFormat:#"%#", [json objectForKey:#"imei"]];
imeiLable.text = jimei;

NSInvalidArgumentException - "unrecognized selector sent to instance " getting error when passing json data IOS7

Hi in my application I'm trying to passing json data in to my tableview but its showing like unrecognized selector sent to instance like this.
I have a json data like this.
"youtube_videos":[{"id":"1","name":"Little Flower Public School","youtube":"FFTi2Sl8hHw","link":"http:\/\/img.youtube.com\/vi\/FFTi2Sl8hHw\/default.jpg"}]
This json have to youtube video id and thumbnail image of the video now I'm trying to view the thumbnail image in my imageview in my tableview but its giving error like
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72e290
This is the code have used to get the json data .
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray =[[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString * dd = [[json objectAtIndex:i]objectForKey:#"youtube"];
NSString * sspp = [[json objectAtIndex:i]objectForKey:#"link"];
NSString * plae =[[json objectAtIndex:i]objectForKey:#"name"];
NSLog(#"%#",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
}
[self.mytableview reloadData];
My Json NSlog.
json :{
"youtube_videos" = (
{
id = 1;
link = "http://img.youtube.com/vi/FFTi2Sl8hHw/default.jpg";
name = "Little Flower Public School";
},
{
id = 2;
link = "http://img.youtube.com/vi/4oI7xC2wSpw/default.jpg";
name = "Little Flower Public School";
},
{
id = 3;
link = "http://img.youtube.com/vi/XCPKMWyF1Lo/default.jpg";
name = Function;
}
);
}
Please Tell me how to resolve this issue i have stuck here for long .
Thanks.
I think you should update your code as like below. Because of your JSON looks like a Dictionary.
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray = [[NSMutableArray alloc]init];
NSArray * respArray = [json objectForKey:#"youtube_videos"];
for (int i=0; i<respArray.count; i++)
{
NSString * dd = [[respArray objectAtIndex:i]objectForKey:#"youtube"];
NSString * sspp = [[respArray objectAtIndex:i]objectForKey:#"link"];
NSString * plae =[[respArray objectAtIndex:i]objectForKey:#"name"];
NSLog(#"%#",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
}
[self.mytableview reloadData];
}
Not sure what your response is. Do you get single or multiple records in the json response.
Try this code:
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray =[[NSMutableArray alloc]init];
NSString * dd = [json objectForKey:#"youtube"];
NSString * sspp = [json objectForKey:#"link"];
NSString * plae =[json objectForKey:#"name"];
NSLog(#"%#",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
[self.mytableview reloadData];
}

Why does my iOS app crash with JSON?

I'm trying to acces my JSON in Objective-C from a higher scope. But it keeps crashing and I have no idea why.
The following code works:
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONOnkectWithData: responseData
options:kNilOptions
error:&error];
NSArray* nameAndPlace = [[json objectForKey:#"objects"] objectForKey:#"havens"];
//NSArray* productArray = [[json objectForKey:#"objects"] objectForKey:#"products"];
NSLog(#"FROM ---> %#", nameAndPlace);
for (NSDictionary *mapPointLoop in nameAndPlace) {
NSString * name = [mapPointLoop objectForKey:#"name"];
NSString * longitudeGet = [mapPointLoop objectForKey:#"longitude"];
NSString * latitudeGet = [mapPointLoop objectForKey:#"latitude"];
myAnnotation *annotation1 = [[myAnnotation alloc] init];
CLLocationCoordinate2D coordinate1;
coordinate1.longitude = longitude;
coordinate1.latitude = latitude;
annotation1.coordinate = coordinate1;
annotation1.title = name;
[mainMap addAnnotation:annotation1];
//NSLog(#"FROM ---> %#", mapPointLoop);
}
}
But when I use this it crashes:
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* nameAndPlace = [json objectForKey:#"objects"];
//NSArray* productArray = [[json objectForKey:#"objects"] objectForKey:#"products"];
NSLog(#"FROM -> %#", nameAndP1ace);
for (NSDictionary *mapPointLoop in nameAndPlace) {
NSString * name = [[mapPointLoop objectForKey:#"havens"] objectForKey:#"name"];
NSNumber * longitudeGet = [mapPointLoop objectForKey:#"longitude"];
NSNumber * latitudeGet = [mapPointLoop objectForKey:#"latitude"];
float latitude = [latitudeGet floatvalue];
float longitude = [longitudeGet floatvalue];
myAnnotation *annotation1 = [[myAnnotation alloc] init];
CLLocationCoordinate2D coordinate1;
coordinate1.longitude = longitude;
coordinate1.latitude = latitude;
annotation1.coordinate = coordinate1
annotation1.title = name;
[mainMap addAnnotation:annotation1];
//NSLog(#"FROM -> %#", mapPointLoop):
}
}
It crashes with the error...
2014-05-20 17:49:57.709 labelAPortTest[5556:60b] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString objectForKey:]: unrecognized selector sent to instance
0x1172426b0'
It crash because when you take the name:
NSString *name = [[mapPointLoop objectForKey:#"havnes"] objectForKey:#"name"];
this:
[mapPointLoop objectForKey:#"havens"]
is an NSArray and not an NSDictionary with the key name.
In fact in your first code you take the objectForKey:#"objects"] objectForKey:#"havens]"
so you have this situation:
NSDictionary ~> objects ~> NSDictionary ~> havens ~> NSArray (of NSDictionary)
then you go to through a for cycle getting 1 to 1 the NSDictionary in the NSArray.
Second case (that crash):
NSDictionary ~> objects ~> NSDictionary
but your are putting that in an NSArray going again with the for cycle..on an NSDictionary.
But it keeps crashing and I have no idea why.
The reason is right here:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1172426b0'
You have an uncaught exception. Furthermore, you have some information about what that exception is, namely that you're sending the objectForKey: message to an object that doesn't implement it.
The first thing you should do is to set a breakpoint for all exceptions:
That'll cause the debugger to stop as soon as the exception is thrown instead of waiting until the app exits, which will give you a lot more context. You should be able to see which object is getting the unknown message. If that doesn't make the problem completely clear, set a breakpoint a line or two before the exception happens and step through the code a line at a time until you find the problem.

Parsing NSDictionary data from JSONValue as a string

I just fetched data but when i try to append it to string from NSData , compiler occurs Exception error .
My code seems like this:
NSDictionary *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];
NSString *jSonStatus = [allDAtaDictionary objectForKey:#"status"];
if([jSonStatus isEqualToString:#"OK"])
{
NSDictionary *results = [allDAtaDictionary objectForKey:#"user_details"];
NSLog(#"User Details : %#",results);
All things works well until these line just starts:
NSString *userID = [results objectForKey:#"userID"];
The error output is :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]:
unrecognized selector sent to instance 0x9485a00'
and this is my jSOn output , NSLog(#"User Details : %#",results);
User Details : (
{
userCity = California;
userCountry = "United States";
userDOB = "1988-03-02";
userDetails = "";
userEmail = "xxx#xxx.com";
userFBID = 715296184;
userFullName = "John Mc Grager";
userGender = Male;
userID = 70;
userLastLoginTime = "2013-05-29 10:51:27";
userLatitude = "37.7858";
userLongtitude = "-122.406";
userNickName = warblader;
userPassword = "";
userStatus = 1;
userToken = "";
}
)
Seems like the JSON is an array with one dictionary in it. What you show us here at least.
So
NSArray *results = [allDAtaDictionary objectForKey:#"user_details"];
NSLog(#"User Details : %#",results);
assert(results.count>0);
NSDictionary *user = [results objectAtIndex:0];
NSString *userID = [results objectForKey:#"userID"];
NSLog(#"%#", userID);
First of all you need save use the serialised data into an array.. Then you have to use it accordingly .. in this case
NSArray *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];
Log the array in index 0 and the see what you are getting. After this you can start parsing the data.
The exception is occuring due to this. Check my answer here to parse JSON How to parse JSON with multiple instance in Object C
Your response probably has An array in it, which result Dictionary is being holding, so you need to use ObjectAtIndex rather calling objectForKey

Resources