Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json parsing+iphone

how to do json parsing in iphone.i had used below way:-

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"viewdidload");
    responseData = [[NSMutableData data] retain]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {        
    [responseData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {    
    NSLog(@"didFailWithError");
    label.text = [NSString stringWithFormat:@"Connection failed: %@",
                                            [error description]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");   
    [connection release];
}

please guide me is it true. And how can i able to get the data of exact tag which we do in xml.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

...and here's how it's done on iPhone:

NSDictionary *feed = responseData;
// get the array of "results" from the feed and cast to NSArray
NSArray *results= (NSArray *)[feed valueForKey:@"results"];

// loop over all the results objects and print their names
int ndx;
NSDictionary *result;
for (ndx = 0; ndx < results.count; ndx++) {
    NSDictionary *result = (NSDictionary *)[results objectAtIndex:ndx];
    NSLog(@"This is the name of this result: %@", [resultvalueForKey:@"name"]); 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...