I'm trying to build an app in which people can order goods from different stores. I am totally new to programming, and I thought building a project like this would be a good way to learn it.
I'm stuck at the part of my app where a user can search for a company (on company name, location, or both) in my database. The database returns a JSON string, containing the company name and location for all found companies, which looks like this:
{"companyName":"testcompany_1","companyCity":"Tiel"},
{"companyName":"tectcompany_2","companyCity":"Tiel"}
So far so good!
now I want to turn this JSON string, which is an NSString
, into an NSDictionary
, in order to display the names and locations of the found companies in a tableView
. This is where I get stuck.
I have been searching through StackOverflow, google, etcetera, and I have tried several ways to do this, for example:
Since none of the tutorials/answers is saw really solves my problem, I tried to make something out of them myself, and this is the result of that:
NSString *strURL = [NSString stringWithFormat:@"http://www.imagine-app.nl/companySearch.php?companyNameSearchField=%@&companyCitySearchField=%@", companyNameSearchField.text, companyCitySearchField.text];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSArray *companies = [inputString componentsSeparatedByString:@"},{"];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:companies] options:kNilOptions error:nil];
To me, this makes sense. First, turn an NSString
into an NSArray
to separate objects, and after that convert the string into an NSDictionary
, which can be used by the tableview
I want to populate with this.
But when I log the dictionary, it says null
, so no NSDictionary
seems to be made by this code.
Now after several weeks of trying and searching, i'm starting to feel pretty stupid and desperate because I cannot find a good way to do this.
Does anyone know bow to turn a json string
as shown above, into an NSDictionary
?