3

I am new to developing and stackoverflow please help me. i am trying to do a simple application where the YQL link is used to get local data and display it in table format. For that i am converting the data into dictionary , later i want to send it into table. But when i tried to convert data to Dictionary it says null. Please help me. Check the Screenshot below. Thanks in advance.

NSString *str = @"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true"; 

Here i took json query into a string (*str)

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]]; 

//   NSString *stringFromData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"%@", stringFromData);

When i tried to implement this commented code im getting the result as expected, but i want to put all the data into dictionary and display it, so i tried to convert the data into dictionary

NSDictionary *dataFromWeb = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


NSDictionary *queryDict = [dataFromWeb objectForKey:@"query"];
NSDictionary *results = [dataFromWeb objectForKey:@"results"];
NSString *allResults = [queryDict objectForKey:@"Results"];

NSLog(@"%@", dataFromWeb);

}

enter image description here

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119

5 Answers5

7

The response returning from Yahoo API is XML by default. You should append format=json to the querystring in order to get the response in JSON format so you can parse it using NSJSONSerialization class:

https://query.yahooapis.com/v1/public/yql?format=json&q=select...
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
1

Your Api Return xml data so you need to do xml parsing

NSJSONSerialization is used for json parsing

NSString *str = @"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true";

    NSURL *UrlFromStr = [NSURL URLWithString:str];

hit this UrlFromStr on your browser you see it return xml data not json

use NSXMLParser to parse xml data

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

Please Use NSXMLParser To Parse xml data instead of NSJSONSerialization. NSJSONSerialization is used to parse JSON data.

Declare in .h file

@property (nonatomic, strong) NSXMLParser *xmlParser;

in .m file.

NSString *str = @"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]]; 

 self.xmlParser = [[NSXMLParser alloc] initWithData:data];
 self.xmlParser.delegate = self;

 // Initialize the mutable string that we'll use during parsing.
 self.foundValue = [[NSMutableString alloc] init];

  // Start parsing.
 [self.xmlParser parse]; 
Ajharudeen khan
  • 246
  • 2
  • 10
0

The format of objects you wanna parse is XML,but you use the class of ParseJSON to parse it ,so it return NULL.You can use the class of NSXML to parse it and then set its delegate to execute the related methods...Good luck to you ..

Marc Steven
  • 477
  • 4
  • 16
0

your Api is not a json its XML format use the XML Parser, visit below link,

http://www.theappguruz.com/blog/xmlparsing-with-nsxmlparser-tutorial

hope its helpful.

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30