如何在JSON中获得价值 - Google API

时间:2015-04-06 11:10:25

标签: objective-c json xcode google-api nsdictionary

我目前正在开发一个在json中恢复数据的应用程序,并将这些应用程序放在一个直接连接到视图的tableview中。 我成功检索数据并将它们放在我的单元格中,但是我遇到了一些数据无法正常处理的问题。

我从google的搜索API获取数据,这是我的请求:

NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=chicha+lyon&sensor=false&key=AIzaSyDfdpFcPFSr-0lTaxsyia84VU0VAsgEt1c"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    self.googlePlacesArrayFromAFNetworking = [responseObject objectForKey:@"results"];

我创建了一个新的dictionnary来存储数据,如下所示:

NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

我的json是:

"results" : [ 

  {
     "formatted_address" : "3 Rue Jangot, 69007 Lyon, France",
     "geometry" : {
        "location" : {
           "lat" : 45.752138,
           "lng" : 4.842072
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
     "id" : "0c5eca79397af5123a9c9642ff73eec2f55680aa",
     "name" : "Le CRISTAL Lounge",
     "opening_hours" : {
        "**open_now" : false,**
        "weekday_text" : []
     },
     "photos" : [
        {
           "height" : 1481,
           "html_attributions" : [],
           "photo_reference" : "CnRnAAAArPewwyAnXVPJcMTaMhJjIYzQMRoFz-WTiFVGkjIGXEfmKxMdWCgFzv9uNa3HPDRqWq0pojuUFbKBt4l0a6zoACU27t0F1noEQoysGnWb7JVTM5lR0uoCCTtumpibtSZpXsECrAHN140s4vAogQAh-RIQ0kuRl7kKzNmTQy6as30x2BoUrNMkQKrMVS2Mnu9pi5tgw7N1UcQ",
           "width" : 2048
        }

我想得到“open_now”的值,但我没有成功:(。 我尝试了很多东西,但我得到的最好的是:

2015-04-06 12:28:56.099 WebTableView [14851:1734075]数组:0

2015-04-06 12:28:56.100 WebTableView [14851:1734075]数组:1

2015-04-06 12:28:56.101 WebTableView [14851:1734075]数组:1

2015-04-06 12:28:56.102 WebTableView [14851:1734075]数组:( null)

2015-04-06 12:28:56.103 WebTableView [14851:1734075]数组:( null)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个,键opening_hours的值是一个字典:

NSMutableDictionary *openhours = [tempdictionnary objectForKey:@"opening_hours"];

BOOL firstvalue = [openhours objectForKey:@"open_now"];