Muhannad Dasoqie
Muhannad Dasoqie

Reputation: 313

Parse JSON - iPhone

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

Upvotes: 0

Views: 207

Answers (2)

superGokuN
superGokuN

Reputation: 1424

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"];


 }

}

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

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.

Upvotes: 5

Related Questions