JSON web file is not overwriting and replacing the local JSON file on the App.
-(void)writeJsonToFile
{
NSURL *fileJSON = [[NSBundle mainBundle] URLForResource:#"data" withExtension:#"json"];
NSString *filePath = [NSString stringWithFormat:#"%#",fileJSON];
NSString *stringURL = #"website.com/data.json";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
The destination for urlData and filePath files match each other.
Been looking around the site and other places online, made sure the file path matched.
We can never change the NSBundle file but
you can save in to local then try this
-(void)writeJsonToFile
{
NSString * filePath=[NSString stringWithFormat:#"%#/Documents/data.json",NSHomeDirectory()];
NSString *stringURL = #"website.com/data.json";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
This code replacing local JSON file when call.
Your JSON file save in your application directory/Documents/ and file name is data.json
website.com/data.json do not return json data, please check it and if needed to change your URL.
Sorry i am really beginner in IPhone development,i am pulling json data from URL and its pulling and loading data perfectly in UITableView, below is code
- (void)fetchFeed
{
NSString *requestString = #"http://bookapi.bignerdranch.com/courses.json";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
self.session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:req
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.courses = jsonObject[#"courses"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
[dataTask resume];
}
Now i want to add filter by instructors, Can any one tell me how i can do that.
Thanks
GET
GET request is just a matter of appending query strings to the API url, for example:
NSString *format = [NSString stringWithFormat:#"http://www.yourapiurl.com?id=%#","123";
NSURL *url = [NSURL URLWithString:format];
NSLog(#"%#",url);
//Creating the data object that will hold the content of the URL
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
POST
-(NSData *)post:(NSString *)postParams{
//Build the Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.yourapiurl.com"]];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%lu", (unsigned long)[postParams length]] forHTTPHeaderField:#"Content-length"];
[request setHTTPBody:[postParams dataUsingEncoding:NSUTF8StringEncoding]];
//Send the Request
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
//Get the Result of Request
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
return returnData;
}
usage:
NSString *postString = [NSString stringWithFormat:#"prop1=%#&prop2=%#&prop3=%#",
"value1","value2","value3"];
NSData *JsonData = [self post :postString];
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:JsonData options:NSJSONReadingMutableContainers error:nil];
//then parse the JSON
I want to show the content depending on user id.
The user log-in to the App, after the successful login, he will see the content depending on his id that is retrieved from the JSON file which is connected the the external mysql database.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *jsonURL = [NSURL URLWithString:#"http://api.example.com/page.php?user_id=1"];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"%#",dataDictionary);
self.something = [dataDictionary objectForKey:#"something"];
}
My problem is :
How to make " 1 " in ( user_id=1 ) dynamic, in a way, it will be changed automatically when the user log-in to his account.
Prepare URL dynamically
NSString *userID = #"23";//Get your userID after login
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://api.example.com/page.php?user_id=%#",userID];
you can use StringWithFormat Method of NSString Class, This methods helps you in creating dynamic NSString Objects,
- (void)viewDidLoad
{
[super viewDidLoad];
NSString userId = #"your user id here";
NSURL *jsonURL = [NSURL URLWithString:[NSString StringWithFormat:#"http://api.example.com/page.php?user_id=%#",userId]];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"%#",dataDictionary);
self.something = [dataDictionary objectForKey:#"something"];
}
So i have this JSON:
{"Curs":[{"ID":"AED","Name":"Dirhamul Emiratelor Arabe","Curs":"0.8013","Multiplier":"1","Data":"2011-03-21"},{"ID":"AUD","Name":"Dolarul australian","Curs":"2.9611","Multiplier":"1","Data":"2011-03-21"},{"ID":"BGN","Name":"Leva bulgareasc","Curs":"2.1314","Multiplier":"1","Data":"2011-03-21"},{"ID":"BRL","Name":"Realul brazilian","Curs":"1.7636","Multiplier":"1","Data":"2011-03-21"},{"ID":"CAD","Name":"Dolarul canadian","Curs":"3.0137","Multiplier":"1","Data":"2011-03-21"},{"ID":"CHF","Name":"Francul elvetian","Curs":"3.2484","Multiplier":"1","Data":"2011-03-21"},{"ID":"CNY","Name":"Renminbi-ul chinezesc","Curs":"0.4483","Multiplier":"1","Data":"2011-03-21"},{"ID":"CZK","Name":"Coroana ceha","Curs":"0.1707","Multiplier":"1","Data":"2011-03-21"},{"ID":"DKK","Name":"Coroana daneza","Curs":"0.559","Multiplier":"1","Data":"2011-03-21"},{"ID":"EGP","Name":"Lira egipteana","Curs":"0.4959","Multiplier":"1","Data":"2011-03-21"},{"ID":"EUR","Name":"Euro","Curs":"4.1685","Multiplier":"1","Data":"2011-03-21"},{"ID":"GBP","Name":"Lira sterlina","Curs":"4.7864","Multiplier":"1","Data":"2011-03-21"},{"ID":"HUF","Name":"100 Forinti maghiari","Curs":"1.5354","Multiplier":"100","Data":"2011-03-21"},{"ID":"INR","Name":"Rupia indiana","Curs":"0.0653","Multiplier":"1","Data":"2011-03-21"},{"ID":"JPY","Name":"100 Yeni japonezi","Curs":"3.6213","Multiplier":"100","Data":"2011-03-21"},{"ID":"KRW","Name":"100 Woni sud-coreeni","Curs":"0.2623","Multiplier":"100","Data":"2011-03-21"},{"ID":"MDL","Name":"Leul moldovenesc","Curs":"0.2507","Multiplier":"1","Data":"2011-03-21"},{"ID":"MXN","Name":"Peso-ul mexican\t","Curs":"0.2452","Multiplier":"1","Data":"2011-03-21"},{"ID":"NOK","Name":"Coroana norvegiana","Curs":"0.5286","Multiplier":"1","Data":"2011-03-21"},{"ID":"NZD","Name":"Dolarul neo-zeelandez","Curs":"2.164","Multiplier":"1","Data":"2011-03-21"},{"ID":"PLN","Name":"Zlotul polonez","Curs":"1.0299","Multiplier":"1","Data":"2011-03-21"},{"ID":"RSD","Name":null,"Curs":"0.0404","Multiplier":"1","Data":"2011-03-21"},{"ID":"RUB","Name":"Rubla ruseasca","Curs":"0.1039","Multiplier":"1","Data":"2011-03-21"},{"ID":"SEK","Name":"Coroana suedeza","Curs":"0.4697","Multiplier":"1","Data":"2011-03-21"},{"ID":"TRY","Name":"Lira turceasca","Curs":"1.8665","Multiplier":"1","Data":"2011-03-21"},{"ID":"UAH","Name":"Hryvna ucraineana","Curs":"0.3699","Multiplier":"1","Data":"2011-03-21"},{"ID":"USD","Name":"Dolarul american","Curs":"2.9428","Multiplier":"1","Data":"2011-03-21"},{"ID":"XAU","Name":"Gramul de aur","Curs":"135.0844","Multiplier":"1","Data":"2011-03-21"},{"ID":"XDR","Name":"DST","Curs":"4.678","Multiplier":"1","Data":"2011-03-21"},{"ID":"ZAR","Name":"Randul sud-african ","Curs":"0.4233","Multiplier":"1","Data":"2011-03-21"}]}
And i want to parse it but i am new to this and don't know how. For example how could i get the values from ID.
Here is my code:
#import "DownloadData.h"
#import "JSON.h"
#implementation DownloadData
-(void) connection
{
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://source"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//NSLog(json_string);
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
// Each element in statuses is a single status
// represented as a NSDictionary
//NSLog([json_string description]);
//NSLog([json_string objectForKey:#"Curs"]);
/*
for (NSDictionary *status in statuses)
{
// You can retrieve individual values using objectForKey on the status NSDictionary
// This will print the tweet and username to the console
NSLog(#"%# - %#",[status objectForKey:#"Curs"]);
}
*/
NSLog(#"test");
//jsondownload=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
#end
Do:
NSDictionary *responseObj = [parser objectWithString:json_string error:nil];
NSArray *statuses = [responseObj objectForKey:#"Curs"];
for (id anUpdate in statuses) {
NSLog(#"ID: %#", [(NSDictionary*)anUpdate objectForKey:#"ID"]);
}