문제

I'm trying to pull in the first url of a tweet using -objectForKey:, and I was wondering how I can pull in the expanded url in 1 go.

Here's the json:

"entities":{
      "hashtags":[

      ],
      "symbols":[

      ],
      "urls":[
         {
            "url":"http:\/\/t.co\/example",
            "expanded_url":"http:\/\/example.com\/hi",
            "display_url":"example.com\/hi",
            "indices":[
               46,
               68
            ]
         }
      ],
      "user_mentions":[

      ]
   },

This is what I tried: NSLog(@"URL FOUND: %@", [JSON objectForKey:@"entities/urls/expanded_url"]); but I got (null).

도움이 되었습니까?

해결책

This code will be help for you

NSArray *urls = [[JSON objectForKey:@"entities"] objectForKey:@"urls"];
NSString *url = [[urls objectAtIndex:0] objectForKey:@"url"];
NSLog(@"%@",url);

다른 팁

"urls":[
     {
        "url":"http:\/\/t.co\/example",
        "expanded_url":"http:\/\/example.com\/hi",
        "display_url":"example.com\/hi",
        "indices":[
           46,
           68
        ]
     }
  ],

Here, value of urls is an array, so you should use path like entities/urls[0]/expanded_url

Update:

I don't know which JSON decode library you use.

Take famous JSONKit(https://github.com/johnezang/JSONKit) as an example:

NSString *str = @"{\"entities\":{\"urls\":[{\"url\":\"http://t.co/example\"}]}}";
NSDictionary *dict = [str objectFromJSONString];
NSLog(@"URL: %@",dict[@"entities"][@"urls"][0][@"url"]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top