iOS - Hosting JSON file to parse it using URL

12,210

Solution 1

Okay, so you want to host a static JSON file on a webserver so an iOS app can open and parse it. There are 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 text-editor 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.

Solution 2

You can still use Dropbox if you don't want to pay for a hosting provider or if you just want to test your app before paying for one.

To do this, you need to replace the www.dropbox.com part of the URL with dl.dropboxusercontent.com as is said in this Dropbox article.

I'm mainly leaving this answer for the future, as this can be useful for other people (me included).

Solution 3

This is exactly the problem I wanted to solve with http://www.myjson.com An easy and simple solution to hosting JSON.

Solution 4

You can save your JSON here https://quarkbackend.com - free json hosting

This tool let you manage your files and the urls will not change after updating the file

Solution 5

Other than myjson.com, You can host it in pastebin.com and use the "Raw" link to access it. Once advantage of pastebin.com over myjson.com is that you can set a expiry period after which it will be auto deleted.

Share:
12,210

Related videos on Youtube

Obj-Swift
Author by

Obj-Swift

iOS,OSX Developer. SOreadytohelp

Updated on September 16, 2022

Comments

  • Obj-Swift
    Obj-Swift over 1 year

    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.

  • Patrick Tescher
    Patrick Tescher over 10 years
    There are a bunch of good resources for this, here is one: chadthompson.me/2013/05/06/static-web-hosting-with-amazon-s3
  • Obj-Swift
    Obj-Swift about 10 years
    Very elegant solution indeed. thanks Lance.
  • Grozz
    Grozz about 9 years
    That's a very unsafe approach. As far as I understand anybody can modify anybody else's JSON.
  • Ashish Sajwan
    Ashish Sajwan over 8 years
    does myjson.com support editting JSON on same URL ??
  • itsji10dra
    itsji10dra almost 7 years
    You are Awesome..
  • Atul Bhardwaj
    Atul Bhardwaj almost 6 years
    @AshishSajwan I think no. Use this link stackoverflow.com/a/50667231/1179638