Reputation: 11201
I'm trying to read my json data, but it crashes.
NSString *hostStr = @"http://www.myIP.com/notices.php";
NSLog(@"%@",hostStr);
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
//NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
id object = [serverOutput JSONValue];
NSMutableDictionary *dictionary = [object JSONRepresentation];
NSLog(@"serverOutput %@", serverOutput);
NSLog(@"Dictionary %@", dictionary);
I got,
2012-06-27 12:47:51.138 usualBike[1520:f803] serverOutput [{"id":"128","title":"Usual Bike llega a Tres Cantos"},{"id":"127","title":"Sol y Bicis "},{"id":"126","title":"Usual Bike reduce los tiempos de espera"}]
2012-06-27 12:47:51.139 usualBike[1520:f803] Dictionary [{"id":"128","title":"Usual Bike llega a Tres Cantos"},{"id":"127","title":"Sol y Bicis "},{"id":"126","title":"Usual Bike reduce los tiempos de espera"}]
Should dictionary be the same than serverOutput?
Now I add this:
for (id key in [dictionary allKeys]) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
And it crashes:
2012-06-27 12:51:40.652 usualBike[1550:f803] -[__NSCFString allKeys]: unrecognized selector sent to instance 0x88683f0
2012-06-27 12:51:40.653 usualBike[1550:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString allKeys]: unrecognized selector sent to instance 0x88683f0'
*** First throw call stack:
(0x13e7022 0x1578cd6 0x13e8cbd 0x134ded0 0x134dcb2 0x2b2a 0xf8a1e 0x1140a9 0x113edd 0x1124aa 0x11234f 0x113e14 0x13e8e99 0x3414e 0x340e6 0x25c4bd 0x13e8e99 0x3414e 0x340e6 0xdaade 0xdafa7 0xdab13 0x25ec48 0x13e8e99 0x3414e 0x340e6 0xdaade 0xdafa7 0xda266 0x593c0 0x595e6 0x3fdc4 0x33634 0x12d1ef5 0x13bb195 0x131fff2 0x131e8da 0x131dd84 0x131dc9b 0x12d07d8 0x12d088a 0x31626 0x1f8d 0x1ef5)
terminate called throwing an exception(lldb)
How can I read the json data?
thank you in advance
Upvotes: 0
Views: 109
Reputation: 8106
try to modify this line.
NSDictionary* object = [serverOutput JSONValue];
//you dont need this
//NSMutableDictionary *dictionary = [object JSONRepresentation];
JSONRepresentation
returned NSString
, and NSString
dont have allKeys
method, that thrown an exception
while JSONValue
returns dictionary
object.
your data now are in object
you can get them like
for (NSString* key in [object allKeys]) {
NSLog(@"key: %@, value: %@", key, [object objectForKey:key]);
}
or by pieces
NSLog(@"id = %@", [object objectForKey:@"id"]);
NSLog(@"id = %@", [object objectForKey:@"title"]);
Upvotes: 2
Reputation: 2253
You have a array of dictionary .. use following code:-
NSArray *object = [serverOutput JSONValue];
now parse it and use as you want:-
for (NSMutableDictionary *dict in object) {
NSLog(@"key: %@", [dict allKeys]);
NSLog(@"value_ID : %@", [dict objectForKey:@"id"]);
NSLog(@"value_title : %@", [dict objectForKey:@"title"]);
}
may this will help you....
Upvotes: 1
Reputation: 2218
Use this code
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response String= %@",responseString);
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:responseString error:nil];
NSString *com=(NSString *) [data objectForKey:@"id"];
NSLog(@"YOur id value= %@",com);
}
Upvotes: 0
Reputation: 371
NSMutableDictionary *complaints =[NSJSONSerialization JSONObjectWithData:respo options:kNilOptions error:&error];
NSMutableArray *JSONAry = [complaints objectForKey:@"posts"];
NSMutableArray *tempary =[[NSMutableArray alloc]init];
for (int i=0;i < [JSONAry count];i++) {
ResultFatch *rs = [[ResultFatch alloc] initWithId:[[JSONAry objectAtIndex:i]objectForKey:@"id"]
title :[[JSONAry objectAtIndex:i] objectForKey:@"title"]];
[tempary addObject:rs];
}
NSLog(@"%@",tempary);
the class ResultFatch is return string
Upvotes: 1
Reputation: 1658
use json library.
SBJsonParser *parser;
NSDictionary *resDict = [parser objectWithString: serverOutput error:nil];
Upvotes: -1