How to turn a JSON into a table/paged list using . Net Core MVC?

Asked

Viewed 234 times

1

I’m studying API access and I’m using the Github API. I want to make a screen in which it is possible to search by name the repositories that match the criteria passed. But Github’s results are huge, as I can do to paginate that list?

Now I’m having trouble reading the JSON returned by the search. In the code below is the class that accesses the API and downloads the JSON

public List<Repositorio> PesquisaRepo(string repoNome) {
        List<Repositorio> repositorios = new List<Repositorio>();
        string urlPesquisa = urlPesquisaRepo + repoNome;

        var client = new WebClient();
        client.Headers.Add("user-agent", this.userAgent);

        var resposta = client.DownloadString(urlPesquisa);

        var repoJSON = JArray.Parse(resposta);
}

On the line var repoJSON = JArray.Parse(resposta); the following error occurs:

Error reading JArray from JsonReader. Current JsonReader item is not an array

The returned JSON is in this format:

    {
  "total_count": 18551,
  "incomplete_results": false,
  "items": [
    {
      "id": 76954504,
      "node_id": "MDEwOlJlcG9zaXRvcnk3Njk1NDUwNA==",
      "name": "react-tetris",
      "full_name": "chvin/react-tetris",
      "owner": {
        "login": "chvin",
        "id": 5383506,
        "node_id": "MDQ6VXNlcjUzODM1MDY=",
        "avatar_url": "https://avatars2.githubusercontent.com/u/5383506?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/chvin",
        "html_url": "https://github.com/chvin",
        "followers_url": "https://api.github.com/users/chvin/followers",
        "following_url": "https://api.github.com/users/chvin/following{/other_user}",
        "gists_url": "https://api.github.com/users/chvin/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/chvin/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/chvin/subscriptions",
        "organizations_url": "https://api.github.com/users/chvin/orgs",
        "repos_url": "https://api.github.com/users/chvin/repos",
        "events_url": "https://api.github.com/users/chvin/events{/privacy}",
        "received_events_url": "https://api.github.com/users/chvin/received_events",
        "type": "User",
        "site_admin": false

How do I access only the "items tag"?

The URL I’m accessing is https://api.github.com/search/repositories?q=tetris

  • post the part of the code where it is difficult, so we can help you.

  • @Marconi Compiler indicates error

  • You have the json class ?

  • @Matheusmiranda The class to create objects with this JSON? I have a class called Repositorio that has some attributes that I want to recover from JSON

  • @Marconi tried this. He says "answer" is a string and has no settings to access a method

  • @Leandrosouza, I understand see my answer.

Show 1 more comment

1 answer

1


Remembering that I copied here and played to txt file with Unicode encoding. The result of ReadAllText is the same as DownloadString. I tested only on the spot.

Follows code:

string json = File.ReadAllText(@"C:\json.txt", Encoding.Unicode);

RootObject result = JsonConvert.DeserializeObject<RootObject>(json);

var somente_tag_items = result.items;

If you want to search by Id (2548674), do so:

var items = result.items.Where(x => x.id == 2548674).ToList(); 

Follows the classes:

public class Owner
{
    public string login { get; set; }
    public int id { get; set; }
    public string node_id { get; set; }
    public string avatar_url { get; set; }
    public string gravatar_id { get; set; }
    public string url { get; set; }
    public string html_url { get; set; }
    public string followers_url { get; set; }
    public string following_url { get; set; }
    public string gists_url { get; set; }
    public string starred_url { get; set; }
    public string subscriptions_url { get; set; }
    public string organizations_url { get; set; }
    public string repos_url { get; set; }
    public string events_url { get; set; }
    public string received_events_url { get; set; }
    public string type { get; set; }
    public bool site_admin { get; set; }
}

public class License
{
    public string key { get; set; }
    public string name { get; set; }
    public string spdx_id { get; set; }
    public string url { get; set; }
    public string node_id { get; set; }
}

public class Item
{
    public int id { get; set; }
    public string node_id { get; set; }
    public string name { get; set; }
    public string full_name { get; set; }
    public Owner owner { get; set; }
    public bool @private { get; set; }
    public string html_url { get; set; }
    public string description { get; set; }
    public bool fork { get; set; }
    public string url { get; set; }
    public string forks_url { get; set; }
    public string keys_url { get; set; }
    public string collaborators_url { get; set; }
    public string teams_url { get; set; }
    public string hooks_url { get; set; }
    public string issue_events_url { get; set; }
    public string events_url { get; set; }
    public string assignees_url { get; set; }
    public string branches_url { get; set; }
    public string tags_url { get; set; }
    public string blobs_url { get; set; }
    public string git_tags_url { get; set; }
    public string git_refs_url { get; set; }
    public string trees_url { get; set; }
    public string statuses_url { get; set; }
    public string languages_url { get; set; }
    public string stargazers_url { get; set; }
    public string contributors_url { get; set; }
    public string subscribers_url { get; set; }
    public string subscription_url { get; set; }
    public string commits_url { get; set; }
    public string git_commits_url { get; set; }
    public string comments_url { get; set; }
    public string issue_comment_url { get; set; }
    public string contents_url { get; set; }
    public string compare_url { get; set; }
    public string merges_url { get; set; }
    public string archive_url { get; set; }
    public string downloads_url { get; set; }
    public string issues_url { get; set; }
    public string pulls_url { get; set; }
    public string milestones_url { get; set; }
    public string notifications_url { get; set; }
    public string labels_url { get; set; }
    public string releases_url { get; set; }
    public string deployments_url { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }
    public DateTime pushed_at { get; set; }
    public string git_url { get; set; }
    public string ssh_url { get; set; }
    public string clone_url { get; set; }
    public string svn_url { get; set; }
    public string homepage { get; set; }
    public int size { get; set; }
    public int stargazers_count { get; set; }
    public int watchers_count { get; set; }
    public string language { get; set; }
    public bool has_issues { get; set; }
    public bool has_projects { get; set; }
    public bool has_downloads { get; set; }
    public bool has_wiki { get; set; }
    public bool has_pages { get; set; }
    public int forks_count { get; set; }
    public object mirror_url { get; set; }
    public bool archived { get; set; }
    public int open_issues_count { get; set; }
    public License license { get; set; }
    public int forks { get; set; }
    public int open_issues { get; set; }
    public int watchers { get; set; }
    public string default_branch { get; set; }
    public double score { get; set; }
}

public class RootObject
{
    public int total_count { get; set; }
    public bool incomplete_results { get; set; }
    public List<Item> items { get; set; }
}
  • 1

    It worked! Thank you!

  • You’re welcome, we’re here for this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.