每个JSON数组c#列表

问题描述 投票:0回答:3

我在这个新的和已找出一些JSON解析的。 从API的json响应看起来是这样的。

{
  count: 100,
  value: [{
        id: "03dd9f56-108f-4e8f-b92e-93df05717464",
        name: "IIBTest",
        url: "http://xxx:8080/tfs/DefaultCollection/_apis/projects/03dd9f56-108f-4e8f-b92e-93df05717464",
        state: "wellFormed",
        revision: 14434848,
        visibility: "private"
      },
      {
        id: "b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
        name: "MS Project POC",
        description: "POC for MS Project with TFS",
        url: "http://xxx/tfs/DefaultCollection/_apis/projects/b7e15034-fc8f-4f7e-866a-cb06f44b12ed",
        state: "wellFormed",
        revision: 14434955,
        visibility: "private"
      },
      {
        id: "59e06621-c5f5-4fd1-9c55-1def541b99d9",
        name: "WorkflowReporting",
        url: "http://xxx/tfs/DefaultCollection/_apis/projects/59e06621-c5f5-4fd1-9c55-1def541b99d9",
        state: "wellFormed",
        revision: 14434591,
        visibility: "private"
      },
      {
        id: "78a802f0-5eee-4bcb-bde9-a764e46f56db",
        name: "iSolutions",
        url: "http://xxx/tfs/DefaultCollection/_apis/projects/78a802f0-5eee-4bcb-bde9-a764e46f56db",
        state: "wellFormed",
        revision: 14434639,
        visibility: "private"
      },
      {
        id: "1f20506a-63a5-486a-a857-fec64d7486a6",
        name: "Training",
        description: "MLITS Training and Learning",
        url: "http://xxx/tfs/DefaultCollection/_apis/projects/1f20506a-63a5-486a-a857-fec64d7486a6",
        state: "wellFormed",
        revision: 14434676,
        visibility: "private"
      },

等我有收集的100个项目。在我的代码,我只是想console.writeline名称:我的代码看起来是这样的。

WebClient wc = new WebClient();
var projectUri = "http://xxx/tfs/defaultcollection/_apis/projects?api-version=3.0";
wc.UseDefaultCredentials = true;
string jsonProjectCollection = wc.DownloadString(projectUri);

var project = JsonConvert.DeserializeObject<Project>(jsonProjectCollection);
var projectname = project.value[0].name;
int count = project.count;

Console.WriteLine(count);
Console.WriteLine(projectname);

该项目名称只给我起的名字是第一个数组中,我怎样才能得到它的步每个数组并打印出每个项目名称的列表。对不起,我在这个新的所以任何帮助将是巨大的。

c# arrays json
3个回答
0
投票

算了,不知道我在想这里是如何。

           for (int intCounter = 0; intCounter < count; intCounter ++)
        {
            var projectname = project.value[intCounter].name;
            Console.WriteLine(projectname);
        }

0
投票

对我来说,它更有意义,而且是反序列化,并保留对象结构更为有用。内部值的对象是一个JSON阵列。您可以使用Json.NET JArray类存储里面有什么价值。

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

所以,反序列化 - >拉阵列出来的物体结构的 - >通过所述阵列迭代。

//pseudo code ahead
foreach(project p in projects)
{
    Console.WriteLine(p.name);
}

或者,直接访问对象,你可以循环做一个简单的。请注意,您也可以在C#中做一个对每个阵列上也是如此。

for(int i = 0; i < project.count; i++)
{
    Console.WriteLine(project.value[i].name);
}

0
投票
var projectCollection = JsonConvert.DeserializeObject<Project>(jsonProjectCollection);
var projects= projectCollection.value;
int count = projectCollection .count;
foreach(var project in projects)
{
   Console.WriteLine(project.name);
}
Console.WriteLine(count);
Console.WriteLine(projectname);
© www.soinside.com 2019 - 2024. All rights reserved.