0

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).

Todd Ditchendorf
  • 11,217
  • 14
  • 69
  • 123
KingPolygon
  • 4,753
  • 7
  • 43
  • 72

2 Answers2

1

This code will be help for you

NSArray *urls = [[JSON objectForKey:@"entities"] objectForKey:@"urls"];
NSString *url = [[urls objectAtIndex:0] objectForKey:@"url"];
NSLog(@"%@",url);
henry4343
  • 3,871
  • 5
  • 22
  • 30
0
"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"]);
icodesign
  • 876
  • 1
  • 6
  • 12