Zhr Zhr - 25 days ago 8
iOS Question

iOS - Hosting JSON file to parse it using URL

I have a .json file with valid data. I want to host it online and use the live url in my app. I tried putting the json file in drop box and tried to verify the json data on http://jsonformatter.curiousconcept.com site but it shows "JSON data URL does not contain JSON data" Is there any other way can I achieve this? Thanks.

Answer

Okay, so you want to host a static JSON file on a webserver so a iOS app can open and parse it. There's a couple of steps and a slight learning curve but from what I'm reading this may help you.

Step 1: Verify that your JSON is valid since it appears that there's some confusion. Open the JSON in a texteditor like notepad. Copy it and paste it in the text area at this site:

http://jsonlint.com/

If you get a parse error - find the line and edit. If you can't do this or the JSON is valid - stop and resolve this problem.

Step 2: While you could use dropbox to do this its not a good idea for a real app. Get a web host. Depending on your basic skill level you can use a cloud provider like Amazon, Heroku, etc. Based on this question - I'd recommend a basic ftp site. There are a ton of free/cheap webhosts.

https://www.google.com/search?q=cheap+web+hosting

The only thing you'll want to watch out with a "free plan" for is that they don't inject ads into your pages. (I'm looking at you GoDaddy.)

Step 3: (assuming you have an iOS app setup)

Add AFNetworking to your project and set up a AFJSONOperation.

http://afnetworking.com/

And use the following code:

  NSURL *url = [NSURL URLWithString:@"http://www.foo.com/bar.json"];

  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){

        NSLog(@"JSON: %@", JSON);


    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        NSLog(@"error: %@ response: %@", error, response);


    }];

[operation start];

Edit: removed link to Dropbox article. Added cheap webhost options.