我将问题提交给Microsoft文档。我请求azure devOps api检索特定组织和项目中托管的所有repo。响应是一个json,如下所示:
{
"count": 3,
"value": [
{
"id": "5febef5a-833d-4e14-b9c0-14cb638f91e6",
"name": "AnotherRepository",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/5febef5a-833d-4e14-b9c0-14cb638f91e6",
"project": {
"id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"name": "Fabrikam-Fiber-Git",
"url": "https://dev.azure.com/fabrikam/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"state": "wellFormed"
},
"remoteUrl": "https://dev.azure.com/fabrikam/Fabrikam-Fiber-Git/_git/AnotherRepository"
},
{
"id": "278d5cd2-584d-4b63-824a-2ba458937249",
"name": "Fabrikam-Fiber-Git",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249",
"project": {
"id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"name": "Fabrikam-Fiber-Git",
"url": "https://dev.azure.com/fabrikam/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"state": "wellFormed"
},
"defaultBranch": "refs/heads/master",
"remoteUrl": "https://dev.azure.com/fabrikam/_git/Fabrikam-Fiber-Git"
},
{
"id": "66efb083-777a-4cac-a350-a24b046be6be",
"name": "TestGit",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/66efb083-777a-4cac-a350-a24b046be6be",
"project": {
"id": "281f9a5b-af0d-49b4-a1df-fe6f5e5f84d0",
"name": "TestGit",
"url": "https://dev.azure.com/fabrikam/_apis/projects/281f9a5b-af0d-49b4-a1df-fe6f5e5f84d0",
"state": "wellFormed"
},
"defaultBranch": "refs/heads/master",
"remoteUrl": "https://dev.azure.com/fabrikam/_git/TestGit"
}
]
}
Microsoft文档说api返回类型GitRepository[]
,所以我想将json反序列化为该类型,但我该怎么做呢??
由于count
和value
属性,我构建了一个HelperClass,如下所示:
public class Rootobject
{
public GitRepository[] value { get; set; }
public int count { get; set; }
}
但在反序列化并转换到该对象之后,GitRepository
的所有道具都为null。
有人能帮我为什么所有属性都为空吗??
编辑:这是反序列化
static async Task Main(string[] args)
{
string accessToken = "personal_access_token";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", string.Empty, accessToken))));
var response = await httpClient.GetStringAsync("https://dev.azure.com/fabrikam/_apis/git/repositories?api-version=5.1");
var responseObject = JsonSerializer.Deserialize<Rootobject>(response);
Console.WriteLine();
}
您是如何进行反序列化的?您在哪里定义了GitRepository
类?您是否尝试过反序列化为字符串以检查是否有任何成员发生了更改?文件来自2016年,此后可能发生了变化。
您可以使用Microsoft提供的nuget包来访问这些API:https://learn.microsoft.com/en-us/azure/devops/integrate/concepts/dotnet-client-libraries?view=azure-devops
我认为您需要的NuGet包是Microsoft.TeamFoundationServer.Client
问题是System.Text.Json.JsonSerializer
默认情况下区分大小写。GitRepository
属性名称是PascalCase,而您提供的json示例包含CamelCase名称。因此JsonSerializer无法正确地反序列化这些属性。
您可以通过提供JsonSerializerOptions
:来覆盖默认设置
var responseObject = JsonSerializer.Deserialize<Rootobject>(response, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
我还注意到project
属性仍然没有被反序列化。我想这是因为GitRepository
有一个名为ProjectReference
(而不是Project
(的属性,并且有一个DataMember属性。
看起来System.Text.Json
不尊重DataMember属性。所以我建议您使用Newtonsoft.Json
库。我测试了它,它运行良好:
var responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(response);