2013-05-12 96 views
-3

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

这是我得到的回应:

[ 
    { 
     "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": "[email protected]: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": "[email protected]: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 
    } 
] 
+0

是的,这里有很多问题的副本。首先研究json.org上的JSON语法(它将花费所有10分钟的时间来学习),然后理解该语法完全转换为NSDictionarys和NSArrays。只需选择你喜欢的解析器。 – 2013-05-13 01:34:04

回答

0

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

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

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

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"); 
} 

里面你cellForRowAtIndexPath的tableview委托方法取决于indexpath拿出字典。

[jsonArray objectAtIndex:indexpath.row];