[英]How to fetch details from NSData
I want to fetch specified data html_instructions
from the below URL. 我想从下面的URL获取指定的数据
html_instructions
。 I could take all data from this URL . 我可以从该URL中获取所有数据。
But I need the specific html_instructions
only. 但我只需要特定的
html_instructions
。 How can I split the data? 如何拆分数据?
I used the following code to convert the URL to NSData: 我使用以下代码将URL转换为NSData:
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
});
Use this as : 将此用作:
NSData* data = [NSData dataWithContentsOfURL:googleRequestURL];
if (data == nil) {
return;
}
NSError* error;
NSMutableDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSLog(@"Json : %@",json);
Hope it helps you. 希望对您有帮助。
I parsed the json from the following url and printed the html instructions try copy this code and try: http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false 我从以下网址解析了json并打印了html指令,然后尝试复制此代码并尝试: http ://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor= false
NSData *receivedData = Received data from url;
NSError* error;
NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:receiveData options:kNilOptions error:&error];
NSArray *allkeys = [parsedJson allKeys];
for(int i = 0; i < allkeys.count; i++){
NSLog(@"############################");
if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
NSArray *arr = [responseJsonValue objectForKey:@"routes"];
NSDictionary *dic = [arr objectAtIndex:0];
NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
NSArray *legs = [dic objectForKey:@"legs"];
NSLog(@"legs array count %d", legs.count);
for(int i = 0; i < legs.count; i++){
NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
for (int i = 0; i < stepsArr.count; i++) {
NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
}
}
}
NSLog(@"############################");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.