0

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.

3
  • Have you tried GetAsync<Rootobject>() instead of client.Execute? RestSharp Doc: restsharp.dev/getting-started/… Commented Aug 24, 2021 at 17:31
  • 1
    What happens when you run it? Is an exception thrown? If so, please add the exception details to your question. Commented Aug 24, 2021 at 17:31
  • do you get a successful HTTP response code ? Commented Aug 24, 2021 at 17:37

2 Answers 2

1

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

and then iterate through jo.value

Sign up to request clarification or add additional context in comments.

1 Comment

@ShahramH.Farmanesh If you think that this is the right answer then honour the author of the post with an upvote.
1

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();

2 Comments

Thank you this works, though I do not understand this part, (v => v.name ). Could you explain?
@ShaneP I am just creating the list of project names. see my updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.