I am trying to get my head around the NSJSONSerialization Class Reference. In the lack of a code-example at the developer.apple.com website, I´m lost. There are millions of examples all around the web with other json libaries, but I haven´t been able to get any of them to work with the latest build of xcode. (I´m running: Version 4.3.1 (4E1019) and testing on iPhone 5.0.1)
I want to fetch the data from the json file into my iphone using a button.
Lets say I get my data from the URL: http://companyurl/jsonfile.json (standard JSON-format)
The jsonfile.json looks somthing like this…;
{
"companylist":
[
{
"company":"Companyname 1",
"telephone":"1234567890",
"url":"http:\/\/www.companyname1.com\/",
"category":"category 1",
"position":"1",
},
{
"company":"Companyname 2",
"telephone":"2345678901",
"url":"http:\/\/www.companyname2.com\/",
"category":"category 2",
"position":"2",
},
{
"company":"Companyname 3",
"telephone":"3456789012",
"url":"http:\/\/www.companyname3.com\/",
"category":"category 3",
"position":"3",
}
]
}
What do I write in my .h, and my .m file?
Thanks for any help! :)
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://your_web_server/your_file...."]];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
NSLog(@"Your JSON Object: %@ Or Error is: %@", response, error);
NOTE: This code works on Xcode 4.2 with iOS 5.01 on simulator and 5.1 on iPad device at the moment