在核心 5.0 中保留 $id 和 $values 免受 API 调用



我正在新的core 5.0中进行开发。

我有以下静态方法来调用API:

public static async Task<T> CallApiAsync<T>(string url, object body, HttpMethod method, string accessToken = null, string accessTokenType = null)
{
HttpResponseMessage result = null;
string responseContent = null;
using (var httpClient = new HttpClient())
{

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url)
{
Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json")
};
AddHeader(accessTokenType, accessToken, httpClient, httpRequestMessage);
result = await httpClient.SendAsync(httpRequestMessage);

responseContent = await result.Content.ReadAsStringAsync();
var settings = new JsonSerializerSettings() {
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
return JsonConvert.DeserializeObject<T>(responseContent, settings);
}
}

然而,我的回答是:

{
"$id": "1",
"bills": {
"$id": "2",
"$values": [
{
"$id": "3",
"id": "FEw3Gcw38GVB",
"organizationId": "1"
},
{
"$id": "3",
"id": "D16FbgYcXr35",
"organizationId": "2"
}
]
}
}

我的课是这样的:

public class BillsRoot    {

[JsonProperty]
public List<Bill> bills { get; set; } 
}

public class Bill    {
[JsonProperty]
public string id { get; set; } 
public string organizationId { get; set; } 
}

如何防止$id&价值观

您是否尝试在属性中显式显示名称?

[JsonProperty("$id")]

最新更新