将API JSON转换为app内部的静态嵌套数组

时间:2014-08-20 23:38:14

标签: ios objective-c json api

我使用的API很快就会被切断,我还需要使用它。

它是ESPN API,我只是用它来获取一个团队列表,所以如果它变得静态就很好。

所以现在我点击了API,但是当他们关闭API时我需要让我的应用程序中的团队仍然可用。

以下是JSON,如何以智能方式将其添加到我的应用程序中?(我使用了Restkit,并在各自的tableView控制器中列出API中的项目)

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

响应机构(足以得出想法):

{
    "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"
            },

视图控制器序列是Sports - >联赛 - >队 所以如果你选择"篮球" ---> " NBA - > "迈阿密热火"。

将根据需要发布任何额外的代码,谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望JSON随您的应用程序一起提供,请将JSON文件放入Xcode项目并将JSON放入其中。要访问它,请执行此操作

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

然后使用NSJSONSerialization作为@HotLicks指出

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