Search code examples
iosjsonobjective-c

Turn API JSON into static nested array inside of app


I use an API that is getting cut off soon, and I need to still use it.

Its the ESPN API, and I just use it to get a list of teams, so its fine if it becomes static.

So right now I hit the API, but when they turn the API off I'll need to have the Teams still available in my app.

Below is the JSON, how would I get this into my app in a smart way? (I used Restkit, and list the items from the API in their respective tableView controller).

Request URI: http://api.espn.com/v1/sports/?apikey=abcdefg

Response Body (enough to get an idea):

{
    "sports": [{
        "name": "baseball",
        "id": 1,
        "uid": "s:1",
        "links": {
            "api": {
                "sports": {
                    "href": "http://api.espn.com/v1/sports/baseball"
                },
                "news": {
                    "href": "http://api.espn.com/v1/sports/baseball/news"
                },
                "notes": {
                    "href": "http://api.espn.com/v1/sports/baseball/news/notes"
                },
                "headlines": {
                    "href": "http://api.espn.com/v1/sports/baseball/news/headlines"
                },
                "events": {
                    "href": "http://api.espn.com/v1/sports/baseball/events"
                }
            }
        },
        "leagues": [{
            "name": "Major League Baseball",
            "abbreviation": "mlb",
            "id": 10,
            "uid": "s:1~l:10",
            "groupId": 9,
            "shortName": "MLB",
            "season": {
                "year": 2014,
                "type": 2,
                "description": "regular",
                "startDate": "2014-03-21T07:00:00Z",
                "endDate": "2014-09-30T06:59:59Z"
            },
            "week": {
                "number": 22,
                "startDate": "2014-08-15T07:00:00Z",
                "endDate": "2014-08-22T06:59:00Z"
            }
        }, {
            "name": "Men's College Baseball",
            "abbreviation": "college-baseball",
            "id": 14,
            "groupId": 14,
            "shortName": "NCAA Men's Baseball"
        }]
    }, {
        "name": "basketball",
        "id": 40,
        "uid": "s:40",
        "links": {
            "api": {
                "sports": {
                    "href": "http://api.espn.com/v1/sports/basketball"
                },
                "news": {
                    "href": "http://api.espn.com/v1/sports/basketball/news"
                },
                "notes": {
                    "href": "http://api.espn.com/v1/sports/basketball/news/notes"
                },
                "headlines": {
                    "href": "http://api.espn.com/v1/sports/basketball/news/headlines"
                },
                "events": {
                    "href": "http://api.espn.com/v1/sports/basketball/events"
                }
            }
        },
        "leagues": [{
            "name": "National Basketball Assoc.",
            "abbreviation": "nba",
            "id": 46,
            "uid": "s:40~l:46",
            "groupId": 7,
            "shortName": "NBA",
            "season": {
                "year": 2014,
                "type": 4,
                "description": "offseason",
                "startDate": "2014-07-01T07:00:00Z",
                "endDate": "2014-10-04T06:59:59Z"
            },
            "week": {
                "number": 10,
                "startDate": "2014-06-21T07:00:00Z",
                "endDate": "2014-06-30T06:59:00Z"
            }
        }, {
            "name": "Women's National Basketball Assoc.",
            "abbreviation": "wnba",
            "id": 59,
            "uid": "s:40~l:59",
            "groupId": 3,
            "shortName": "WNBA",
            "season": {
                "year": 2014,
                "type": 3,
                "description": "post",
                "startDate": "2014-08-18T07:00:00Z",
                "endDate": "2014-10-21T06:59:59Z"
            },
            "week": {
                "number": 3,
                "startDate": "2012-10-09T07:00:00Z",
                "endDate": "2012-10-16T06:59:00Z"
            }
        }, {
            "name": "NCAA Basketball",
            "abbreviation": "mens-college-basketball",
            "id": 41,
            "uid": "s:40~l:41",
            "groupId": 52,
            "shortName": "NCAA Basketball",
            "season": {
                "year": 2015,
                "type": 1,
                "description": "pre",
                "startDate": "2014-07-01T07:00:00Z",
                "endDate": "2014-11-07T07:59:59Z"
            },
            "week": {
                "number": 4,
                "startDate": "2014-04-07T07:00:00Z",
                "endDate": "2014-04-14T06:59:00Z"
            }
        }, {
            "name": "NCAA Women's Basketball",
            "abbreviation": "womens-college-basketball",
            "id": 54,
            "uid": "s:40~l:54",
            "groupId": 52,
            "shortName": "NCAA Women's Basketball",
            "season": {
                "year": 2015,
                "type": 1,
                "description": "pre",
                "startDate": "2014-07-01T07:00:00Z",
                "endDate": "2014-11-07T07:59:59Z"
            },
            "week": {
                "number": 4,
                "startDate": "2014-04-07T07:00:00Z",
                "endDate": "2014-04-14T06:59:00Z"
            }
        }]
    }, {
        "name": "football",
        "id": 20,
        "uid": "s:20",
        "links": {
            "api": {
                "sports": {
                    "href": "http://api.espn.com/v1/sports/football"
                },
                "news": {
                    "href": "http://api.espn.com/v1/sports/football/news"
                },
                "notes": {
                    "href": "http://api.espn.com/v1/sports/football/news/notes"
                },
                "headlines": {
                    "href": "http://api.espn.com/v1/sports/football/news/headlines"
                },
                "events": {
                    "href": "http://api.espn.com/v1/sports/football/events"
                }
            }
        },
        "leagues": [{
            "name": "National Football League",
            "abbreviation": "nfl",
            "id": 28,
            "uid": "s:20~l:28",
            "groupId": 9,
            "shortName": "NFL",
            "season": {
                "year": 2014,
                "type": 1,
                "description": "pre",
                "startDate": "2014-07-30T07:00:00Z",
                "endDate": "2014-09-03T06:59:59Z"
            },
            "week": {
                "number": 4,
                "startDate": "2014-08-20T07:00:00Z",
                "endDate": "2014-08-27T06:59:00Z",
                "text": "Week 3"
            }
        }, {
            "name": "NCAA Football",
            "abbreviation": "college-football",
            "id": 23,
            "uid": "s:20~l:23",
            "groupId": 99,
            "shortName": "NCAA Football",
            "season": {
                "year": 2014,
                "type": 2,
                "description": "regular",
                "startDate": "2014-07-20T07:00:00Z",
                "endDate": "2014-12-16T07:59:59Z"
            },
            "week": {
                "number": 1,
                "startDate": "2014-08-19T07:00:00Z",
                "endDate": "2014-09-02T06:59:00Z",
                "text": "Week 1"
            }
        }]
    }, {
        "name": "hockey",
        "id": 70,
        "uid": "s:70",
        "links": {
            "api": {
                "sports": {
                    "href": "http://api.espn.com/v1/sports/hockey"
                },
                "news": {
                    "href": "http://api.espn.com/v1/sports/hockey/news"
                },
                "notes": {
                    "href": "http://api.espn.com/v1/sports/hockey/news/notes"
                },
                "headlines": {
                    "href": "http://api.espn.com/v1/sports/hockey/news/headlines"
                },
                "events": {
                    "href": "http://api.espn.com/v1/sports/hockey/events"
                }
            }
        },
        "leagues": [{
            "name": "National Hockey League",
            "abbreviation": "nhl",
            "id": 90,
            "uid": "s:70~l:90",
            "groupId": 9,
            "shortName": "NHL",
            "season": {
                "year": 2014,
                "type": 4,
                "description": "offseason",
                "startDate": "2014-06-23T07:00:00Z",
                "endDate": "2014-09-01T06:59:59Z"
            },

The view controller sequence is Sports --> Leagues --> Teams So if you pick "Basketball" ---> "NBA --> "Miami Heat".

Will post any extra code as needed.


Solution

  • If you want the JSON to ship with your app, put a JSON file in your Xcode project and put your JSON in it. To access it, do this

    NSString *JSONFilePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"json"];
    

    and then use NSJSONSerialization as @HotLicks pointed out

    NSData *JSONData = [NSData dataWithContentsOfFile:JSONFilePath];
    NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];