Sherpa11
Sherpa11

Reputation: 179

Load JSON object into array?

I'm having trouble loading this json object into an array.

Here is a snippet (fragment) of my JSON response from the API:

{
   "count":192,
   "value":[
      {
         "id":"03dd9f56-108f-4e8f-b92e-93df05717464",
         "name":"IIBTest",
         "url":"https://devops.com/tfs/DefaultCollection/_apis/projects/03dd9f56-108f-4e8f-b92e-93df05717464",
         "state":"wellFormed",
         "revision":14434848,
         "visibility":"private",
         "lastUpdateTime":"2016-08-19T12:21:37.187Z"
      },
      {
         "id":"b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
         "name":"MS Project POC",
         "description":"POC for MS Project with TFS",
         "url":"https://devops.com/tfs/DefaultCollection/_apis/projects/b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
         "state":"wellFormed",
         "revision":14434955,
         "visibility":"private",
         "lastUpdateTime":"2017-10-03T19:31:56.56Z"
      },
      {
         "id":"59e06621-c5f5-4fd1-9c55-1def541b99d9",
         "name":"WorkflowReporting",
         "url":"https://devops.com/tfs/DefaultCollection/_apis/projects/59e06621-c5f5-4fd1-9c55-1def541b99d9",
         "state":"wellFormed",
         "revision":14434591,
         "visibility":"private",
         "lastUpdateTime":"2015-09-11T06:59:12.21Z"
      },
      {
         "id":"78a802f0-5eee-4bcb-bde9-a764e46f56db",
         "name":"iSolutions",
         "description":"",
         "url":"https://devops.com/tfs/DefaultCollection/_apis/projects/78a802f0-5eee-4bcb-bde9-a764e46f56db",
         "state":"wellFormed",
         "revision":14435476,
         "visibility":"private",
         "lastUpdateTime":"2021-08-05T17:17:26.193Z"
      },
      {
         "id":"1f20506a-63a5-486a-a857-fec64d7486a6",
         "name":"Training",
         "description":"MLITS Training and Learning",
         "url":"https://devops.com/tfs/DefaultCollection/_apis/projects/1f20506a-63a5-486a-a857-fec64d7486a6",
         "state":"wellFormed",
         "revision":14435350,
         "visibility":"private",
         "lastUpdateTime":"2021-04-08T22:48:02.923Z"
      },
      ...
}

And here is my code:

public class Rootobject
{
    public int count { get; set; }
    public Value[] value { get; set; }
}

public class Value
{
    public string id { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public string state { get; set; }
    public int revision { get; set; }
    public string visibility { get; set; }
    public DateTime lastUpdateTime { get; set; }
    public string description { get; set; }
}
static void Main(string[] args)
{
    var client = new RestClient("https://devops.americannational.com/tfs/defaultcollection/_apis/projects?$top=300&api-version=5.0")
    {
        Authenticator = new RestSharp.Authenticators.NtlmAuthenticator()
    };

    client.Timeout = -1;
    var request = new RestRequest(Method.GET);
    IRestResponse response = client.Execute(request);
    var jsonString = response.Content;
    var jo = JObject.Parse(jsonString);
    //...
}
        

I am wanting to load the names of the projects into the Array so that I can later iterate through them. Any help is appreciated, I have tried a few things but not having much luck.

Upvotes: 0

Views: 97

Answers (2)

Serge
Serge

Reputation: 43900

try this

var jsonDeserialized= JsonConvert.DeserializeObject<Rootobject>(json);

as example how to use it this query returns array of project names

var projectNames= jsonDeserialized.value.Select(v => v.name ).ToArray();

output

["IIBTest","MS Project POC","WorkflowReporting","iSolutions","Training"]

this returns a list of projects

var projects= jsonDeserialized.value.ToList();

Upvotes: 1

aamd
aamd

Reputation: 427

you can use var jo = JsonConvert.DeserializeObject<Rootobject>(jsonString);

and then iterate through jo.value

Upvotes: 1

Related Questions