-3

如何将来自 GitHub API 的 JSON 结果转换为 NSArray,以便填充 UITableView?

这是我得到的回复:

[
    {
        "archive_url": "https://api.github.com/repos/JamesKTest/Repo1/{archive_format}{/ref}",
        "assignees_url": "https://api.github.com/repos/JamesKTest/Repo1/assignees{/user}",
        "blobs_url": "https://api.github.com/repos/JamesKTest/Repo1/git/blobs{/sha}",
        "branches_url": "https://api.github.com/repos/JamesKTest/Repo1/branches{/branch}",
        "clone_url": "https://github.com/JamesKTest/Repo1.git",
        "collaborators_url": "https://api.github.com/repos/JamesKTest/Repo1/collaborators{/collaborator}",
        "comments_url": "https://api.github.com/repos/JamesKTest/Repo1/comments{/number}",
        "commits_url": "https://api.github.com/repos/JamesKTest/Repo1/commits{/sha}",
        "compare_url": "https://api.github.com/repos/JamesKTest/Repo1/compare/{base}...{head}",
        "contents_url": "https://api.github.com/repos/JamesKTest/Repo1/contents/{+path}",
        "contributors_url": "https://api.github.com/repos/JamesKTest/Repo1/contributors",
        "created_at": "2013-05-12T14:16:14Z",
        "default_branch": "master",
        "description": "Testing Repo 1",
        "downloads_url": "https://api.github.com/repos/JamesKTest/Repo1/downloads",
        "events_url": "https://api.github.com/repos/JamesKTest/Repo1/events",
        "fork": 0,
        "forks": 0,
        "forks_count": 0,
        "forks_url": "https://api.github.com/repos/JamesKTest/Repo1/forks",
        "full_name": "JamesKTest/Repo1",
        "git_commits_url": "https://api.github.com/repos/JamesKTest/Repo1/git/commits{/sha}",
        "git_refs_url": "https://api.github.com/repos/JamesKTest/Repo1/git/refs{/sha}",
        "git_tags_url": "https://api.github.com/repos/JamesKTest/Repo1/git/tags{/sha}",
        "git_url": "git://github.com/JamesKTest/Repo1.git",
        "has_downloads": 1,
        "has_issues": 1,
        "has_wiki": 1,
        "homepage": "<null>",
        "hooks_url": "https://api.github.com/repos/JamesKTest/Repo1/hooks",
        "html_url": "https://github.com/JamesKTest/Repo1",
        "id": 10014504,
        "issue_comment_url": "https://api.github.com/repos/JamesKTest/Repo1/issues/comments/{number}",
        "issue_events_url": "https://api.github.com/repos/JamesKTest/Repo1/issues/events{/number}",
        "issues_url": "https://api.github.com/repos/JamesKTest/Repo1/issues{/number}",
        "keys_url": "https://api.github.com/repos/JamesKTest/Repo1/keys{/key_id}",
        "labels_url": "https://api.github.com/repos/JamesKTest/Repo1/labels{/name}",
        "language": "<null>",
        "languages_url": "https://api.github.com/repos/JamesKTest/Repo1/languages",
        "master_branch": "master",
        "merges_url": "https://api.github.com/repos/JamesKTest/Repo1/merges",
        "milestones_url": "https://api.github.com/repos/JamesKTest/Repo1/milestones{/number}",
        "mirror_url": "<null>",
        "name": "Repo1",
        "notifications_url": "https://api.github.com/repos/JamesKTest/Repo1/notifications{?since,all,participating}",
        "open_issues": 0,
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://secure.gravatar.com/avatar/c0adbfe21e23cdb5165a212c412b6f30?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
            "events_url": "https://api.github.com/users/JamesKTest/events{/privacy}",
            "followers_url": "https://api.github.com/users/JamesKTest/followers",
            "following_url": "https://api.github.com/users/JamesKTest/following{/other_user}",
            "gists_url": "https://api.github.com/users/JamesKTest/gists{/gist_id}",
            "gravatar_id": "c0adbfe21e23cdb5165a212c412b6f30",
            "html_url": "https://github.com/JamesKTest",
            "id": 4395917,
            "login": "JamesKTest",
            "organizations_url": "https://api.github.com/users/JamesKTest/orgs",
            "received_events_url": "https://api.github.com/users/JamesKTest/received_events",
            "repos_url": "https://api.github.com/users/JamesKTest/repos",
            "starred_url": "https://api.github.com/users/JamesKTest/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/JamesKTest/subscriptions",
            "type": "User",
            "url": "https://api.github.com/users/JamesKTest"
        },
        "permissions": {
            "admin": 1,
            "pull": 1,
            "push": 1
        },
        "private": 0,
        "pulls_url": "https://api.github.com/repos/JamesKTest/Repo1/pulls{/number}",
        "pushed_at": "2013-05-12T14:16:15Z",
        "size": 0,
        "ssh_url": "git@github.com:JamesKTest/Repo1.git",
        "stargazers_url": "https://api.github.com/repos/JamesKTest/Repo1/stargazers",
        "statuses_url": "https://api.github.com/repos/JamesKTest/Repo1/statuses/{sha}",
        "subscribers_url": "https://api.github.com/repos/JamesKTest/Repo1/subscribers",
        "subscription_url": "https://api.github.com/repos/JamesKTest/Repo1/subscription",
        "svn_url": "https://github.com/JamesKTest/Repo1",
        "tags_url": "https://api.github.com/repos/JamesKTest/Repo1/tags",
        "teams_url": "https://api.github.com/repos/JamesKTest/Repo1/teams",
        "trees_url": "https://api.github.com/repos/JamesKTest/Repo1/git/trees{/sha}",
        "updated_at": "2013-05-12T14:16:15Z",
        "url": "https://api.github.com/repos/JamesKTest/Repo1",
        "watchers": 0,
        "watchers_count": 0
    },
    {
        "archive_url": "https://api.github.com/repos/JamesKTest/Repo2/{archive_format}{/ref}",
        "assignees_url": "https://api.github.com/repos/JamesKTest/Repo2/assignees{/user}",
        "blobs_url": "https://api.github.com/repos/JamesKTest/Repo2/git/blobs{/sha}",
        "branches_url": "https://api.github.com/repos/JamesKTest/Repo2/branches{/branch}",
        "clone_url": "https://github.com/JamesKTest/Repo2.git",
        "collaborators_url": "https://api.github.com/repos/JamesKTest/Repo2/collaborators{/collaborator}",
        "comments_url": "https://api.github.com/repos/JamesKTest/Repo2/comments{/number}",
        "commits_url": "https://api.github.com/repos/JamesKTest/Repo2/commits{/sha}",
        "compare_url": "https://api.github.com/repos/JamesKTest/Repo2/compare/{base}...{head}",
        "contents_url": "https://api.github.com/repos/JamesKTest/Repo2/contents/{+path}",
        "contributors_url": "https://api.github.com/repos/JamesKTest/Repo2/contributors",
        "created_at": "2013-05-12T14:16:32Z",
        "default_branch": "master",
        "description": "Test Repo 2",
        "downloads_url": "https://api.github.com/repos/JamesKTest/Repo2/downloads",
        "events_url": "https://api.github.com/repos/JamesKTest/Repo2/events",
        "fork": 0,
        "forks": 0,
        "forks_count": 0,
        "forks_url": "https://api.github.com/repos/JamesKTest/Repo2/forks",
        "full_name": "JamesKTest/Repo2",
        "git_commits_url": "https://api.github.com/repos/JamesKTest/Repo2/git/commits{/sha}",
        "git_refs_url": "https://api.github.com/repos/JamesKTest/Repo2/git/refs{/sha}",
        "git_tags_url": "https://api.github.com/repos/JamesKTest/Repo2/git/tags{/sha}",
        "git_url": "git://github.com/JamesKTest/Repo2.git",
        "has_downloads": 1,
        "has_issues": 1,
        "has_wiki": 1,
        "homepage": "<null>",
        "hooks_url": "https://api.github.com/repos/JamesKTest/Repo2/hooks",
        "html_url": "https://github.com/JamesKTest/Repo2",
        "id": 10014510,
        "issue_comment_url": "https://api.github.com/repos/JamesKTest/Repo2/issues/comments/{number}",
        "issue_events_url": "https://api.github.com/repos/JamesKTest/Repo2/issues/events{/number}",
        "issues_url": "https://api.github.com/repos/JamesKTest/Repo2/issues{/number}",
        "keys_url": "https://api.github.com/repos/JamesKTest/Repo2/keys{/key_id}",
        "labels_url": "https://api.github.com/repos/JamesKTest/Repo2/labels{/name}",
        "language": "<null>",
        "languages_url": "https://api.github.com/repos/JamesKTest/Repo2/languages",
        "master_branch": "master",
        "merges_url": "https://api.github.com/repos/JamesKTest/Repo2/merges",
        "milestones_url": "https://api.github.com/repos/JamesKTest/Repo2/milestones{/number}",
        "mirror_url": "<null>",
        "name": "Repo2",
        "notifications_url": "https://api.github.com/repos/JamesKTest/Repo2/notifications{?since,all,participating}",
        "open_issues": 0,
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://secure.gravatar.com/avatar/c0adbfe21e23cdb5165a212c412b6f30?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
            "events_url": "https://api.github.com/users/JamesKTest/events{/privacy}",
            "followers_url": "https://api.github.com/users/JamesKTest/followers",
            "following_url": "https://api.github.com/users/JamesKTest/following{/other_user}",
            "gists_url": "https://api.github.com/users/JamesKTest/gists{/gist_id}",
            "gravatar_id": "c0adbfe21e23cdb5165a212c412b6f30",
            "html_url": "https://github.com/JamesKTest",
            "id": 4395917,
            "login": "JamesKTest",
            "organizations_url": "https://api.github.com/users/JamesKTest/orgs",
            "received_events_url": "https://api.github.com/users/JamesKTest/received_events",
            "repos_url": "https://api.github.com/users/JamesKTest/repos",
            "starred_url": "https://api.github.com/users/JamesKTest/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/JamesKTest/subscriptions",
            "type": "User",
            "url": "https://api.github.com/users/JamesKTest"
        },
        "permissions": {
            "admin": 1,
            "pull": 1,
            "push": 1
        },
        "private": 0,
        "pulls_url": "https://api.github.com/repos/JamesKTest/Repo2/pulls{/number}",
        "pushed_at": "2013-05-12T14:16:36Z",
        "size": 0,
        "ssh_url": "git@github.com:JamesKTest/Repo2.git",
        "stargazers_url": "https://api.github.com/repos/JamesKTest/Repo2/stargazers",
        "statuses_url": "https://api.github.com/repos/JamesKTest/Repo2/statuses/{sha}",
        "subscribers_url": "https://api.github.com/repos/JamesKTest/Repo2/subscribers",
        "subscription_url": "https://api.github.com/repos/JamesKTest/Repo2/subscription",
        "svn_url": "https://github.com/JamesKTest/Repo2",
        "tags_url": "https://api.github.com/repos/JamesKTest/Repo2/tags",
        "teams_url": "https://api.github.com/repos/JamesKTest/Repo2/teams",
        "trees_url": "https://api.github.com/repos/JamesKTest/Repo2/git/trees{/sha}",
        "updated_at": "2013-05-12T14:16:36Z",
        "url": "https://api.github.com/repos/JamesKTest/Repo2",
        "watchers": 0,
        "watchers_count": 0
    }
]
4

2 回答 2

0

由于您的响应包含字典数组,因此在解析 json 后,您将获得一个NSArry字典。

// handle it
NSError *e = nil; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; 

if (!jsonArray) { 
    NSLog(@"Error parsing JSON: %@", e); 
} else { 
    NSLog(@"parsing done and array contains data");
} 

在您的cellForRowAtIndexPathtableview 委托方法中,根据 indexpath 取出字典。

[jsonArray objectAtIndex:indexpath.row];
于 2013-05-12T23:37:56.913 回答
0

您返回的 JSON 结果本质上是一个 NSDictionary。在你的

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

拉出您想要的单个字典条目并将单元格文本设置为从字典返回的 NSString。您的特定行的 indexPath 可能会映射到字典的键,以使您的生活更轻松。

于 2013-05-12T15:25:36.017 回答