【发布时间】:2019-02-05 18:29:15
【问题描述】:
我是这方面的新手,并且已经弄清楚了一些 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);
项目名称只给了我第一个数组中的名称,我怎样才能让它逐步遍历每个数组并打印每个项目名称的列表。抱歉,我是新手,所以任何帮助都会很棒。
【问题讨论】:
-
推测
project.value是一个列表或数组,所以使用foreach (var entry in project.value)等 -
还在问题中添加 Project 类