0

I'm new with json, and I need your help please.

I received JSON string like this :

{"network":
   {
   "network_id":111,
   "name":"test name",
   "city":"test city",
   "country":"test country",
   "description":"test desc"
   }
}

How I can handle this string and split key/value in order to use them in my view ?

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {    
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

    self.responseData = nil;

//*********** How I can parse responseString *********//

[networkIDLabel setText:@"ADD THE VALUE"];
[nameLabel setText:@"ADD THE VALUE"];


    [responseString release];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

thanks

Zaraki
  • 3,720
  • 33
  • 39
Muhannad Dasoqie
  • 313
  • 3
  • 17

2 Answers2

5

In iOS 5 and later, you can parse the response data directly with NSJSONSerialization:

[NSJSONSerialization JSONObjectWithData:self.responseData …];

If you want to support earlier versions of iOS, you can use JSONKit.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

In objective-c json can be represnted as Dictionary

-(void)getData:(NSData*)response{

 // You have to include the SBJSON or else you can also use the NSJSONSerialization

 //NSDictionary *jsonData          =           [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&erro];

SBJSON *parse                               =           [[SBJSON alloc]init];

NSString *jsonString                        =           [[NSString alloc] initWithData:response
                                                                              encoding:NSUTF8StringEncoding]; 

NSDictionary *jsonData                      =           [parse objectWithString:jsonString error:&erro];
NSDictionary *insideData                          =           [jsonData objectForKey:@"network"];

if(![insideData isKindOfClass:[NSNull class]])
{

        NSString *data1         =       [insideData objectForKey:@"network_Id"];

         NSString *data2         =       [insideData objectForKey:@"name"];


 }

}
superGokuN
  • 1,399
  • 13
  • 28
  • You have nested json or can say nested Dictionary so i edited my answer according yo your json – superGokuN Jul 17 '12 at 12:13
  • Thank you naveen :) .. I know that the question seems easy, but I don't familiar with NSDictionary so I can't solve it .. thank you again :)) – Muhannad Dasoqie Jul 17 '12 at 12:20
  • I can print data1 on console, but I cannot used it with labels !! [label setText:[insideData objectForKey:@"network_Id"]]; then I have a crash :( – Muhannad Dasoqie Jul 18 '12 at 06:15
  • -[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x6a5e530 2012-07-18 10:00:53.876 OAuthExample[839:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x6a5e530' *** First throw call stack: (0x1400022 0x1591cd6 0x1401cbd 0x1366ed0 0x1366cb2 0x18a0ff 0x1b3d5 0xa4da49 0xa4be84 0xa4cea7 0xa4be3f 0xa4bfc5 0x990f5a 0x39d3a39 0x3aa0596 0x39ca120 0x3aa0117 0x39c9fbf 0x13d494f 0x1337b43 0x1337424 0x1336d84 0x1336c9b 0x12e97d8 0x12e988a 0x4a626 0x5932 0x28a5) – Muhannad Dasoqie Jul 18 '12 at 07:01
  • - (void) connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; NSDictionary *dic = [responseString JSONValue]; NSLog(@"%@", [[dic objectForKey:@"network"] objectForKey:@"network_id"]); // works perfectly ! [networkIDLabel setText:[[dic objectForKey:@"network"] objectForKey:@"network_id"]]; // crash my app !! self.responseData = nil;} – Muhannad Dasoqie Jul 18 '12 at 07:02
  • 1
    [networkIDLabel setText:[[dic objectForKey:@"network"] objectForKey:@"network_id"]]; dont do like this try this NSString *networkId = [dic objectForKey:@"network_ID"]; [networkIDLabel setText:[NSString stringWithFormat:@"%@",networkId]]; – superGokuN Jul 18 '12 at 07:07
  • OMG !! [networkIDLabel setText:[NSString stringWithFormat:@"%@",[[dic objectForKey:@"network"] objectForKey:@"network_id"]]]; // it's worked without crashing !! Thanks again Naveen :)) – Muhannad Dasoqie Jul 18 '12 at 07:32