0

Hello Stackoverflow Community, I recently got into C# and JSON.net and my task is to filter the repositories I receive from a website. I am working with the Crucible API.

client.Authenticator = new HttpBasicAuthenticator(User, Password);
var request = new RestRequest("", Method.GET);
Console.Clear();

client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

Console.Read();

How would i filter out everything besides the displayNames of the repositories i recieve in my console application? The current output looks like this:

{
    "repoData": [{
            "name": "Example",
            "displayName": "Example",
            "type": "git",
            "enabled": true,
            "available": true,
            "location": "Example.com",
            "path": ""
        }
    ]
}
Paul Karam
  • 4,052
  • 8
  • 30
  • 53
  • your json is not valid, try to add valid json. bcoz your array and outer curly brace not closed – er-sho Oct 04 '18 at 06:02
  • "How would i filter out the displayNames of the repositories i recieve in my console application?" -> you mean you want to display the JSOn but without the name/displayName properties? – ProgrammingLlama Oct 04 '18 at 06:05
  • Sorry @John i formulated the question wrong; I only want to display the DisplayName properties. I edited the post accordingly. – Hollowpoint186 Oct 04 '18 at 06:10
  • 1) Deserialize the JSON to C# classes. 2) Use LINQ's `.Select` on the list of repositories to extract only the name field. - Basically, look at the links ManishM provided, and if you get update your question showing how far you got. – ProgrammingLlama Oct 04 '18 at 06:11
  • @Hollowpoint, does your json contains only single object in your array? – er-sho Oct 04 '18 at 06:14
  • @ershoaib No it contains a lot of objects, I just put this single one as an example for better readability. – Hollowpoint186 Oct 04 '18 at 06:19
  • @Hollowpoint, so you have to read displayName for only first object or all from lot of objects – er-sho Oct 04 '18 at 06:22
  • @ershoaib from all objects – Hollowpoint186 Oct 04 '18 at 06:29

2 Answers2

1

Create a quick type for your json

public class RepoData
{
    public string name { get; set; }
    public string displayName { get; set; }
    public string type { get; set; }
    public bool enabled { get; set; }
    public bool available { get; set; }
    public string location { get; set; }
    public string path { get; set; }
}

public class RootObject4
{
    public List<RepoData> repoData { get; set; }
}

Here i create an console app for your demonstration purpose

class Program
{
    static void Main(string[] args)
    {
        var json = @"{'repoData':[{'name':'Example','displayName':'Example1','type':'git','enabled':true,'available':true,'location':'Example.com','path':''}, {'name':'Example','displayName':'Example2','type':'git','enabled':true,'available':true,'location':'Example.com','path':''}]}";

        RootObject4 rootObject4 = JsonConvert.DeserializeObject<RootObject4>(json);

        List<string> displayNames = new List<string>();

        foreach (var item in rootObject4.repoData)
        {
            displayNames.Add(item.displayName);
        }

        displayNames.ForEach(x => Console.WriteLine(x));

        Console.ReadLine();
    }
}

Alternative: If you don't want to create any classes then JObject will better handle your json and you will extract displayName values by below code sample console app.

class Program
{
    static void Main(string[] args)
    {
        var json = @"{'repoData':[{'name':'Example','displayName':'Example1','type':'git','enabled':true,'available':true,'location':'Example.com','path':''}, {'name':'Example','displayName':'Example2','type':'git','enabled':true,'available':true,'location':'Example.com','path':''}]}";

        JObject jObject = JObject.Parse(json);

        var repoData = jObject["repoData"];

        var displayNames = repoData.Select(x => x["displayName"]).Values().ToList();

        displayNames.ForEach(x => Console.WriteLine(x));

        Console.ReadLine();
    }
}

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

You can use JsonConvert.DeserializeObject. Example is given below

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

By the way, your JSON is incomplete. Please add the complete output so others can help better.

ManishM
  • 583
  • 5
  • 7