c# - 解析反序列化的 Json 数据失败 - c#
问题描述
我有这个json 数据要解析,但我得到解析错误,可以请一些说明吗?
这是我收到时要解析的 Json 数据:
{
"id": "/subscriptions/xxxx",
"type": "Microsoft.ApiManagement/service/users",
"name": "bbbbb",
"properties": {
"firstName": "aaaa",
"lastName": "cccc",
"email": "someone@somewhere.xyz",
"state": "active",
"registrationDate": "somedate",
"note": null,
"groups": [],
"identities": [
{
"provider": "Basic",
"id": "someone@somewhere.xyz"
}
]
}
}
这些是我创建的用于将数据反序列化为的类:
public class NewUserAPIMResultingData
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string thisType { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("properties")]
public NewUserAPIMResultingDataProperties properties { get; set; }
}
public class NewUserAPIMResultingDataProperties
{
[JsonProperty("firstName")]
public string userFirstName { get; set; }
[JsonProperty("lastName")]
public string userLastName { get; set; }
[JsonProperty("email")]
public string userEmail { get; set; }
[JsonProperty("state")]
public string state { get; set; }
[JsonProperty("registrationDate")]
public string registrationDate { get; set; }
[JsonProperty("note")]
public string note { get; set; }
[JsonProperty("groups")]
public IEnumerable<string> groups { get; set; }
[JsonProperty("identities")]
public IEnumerable<NewUserAPIMResultingDataPropertyIdentity> identities { get; set; }
}
public class NewUserAPIMResultingDataPropertyIdentity
{
[JsonProperty("provider")]
public string provider { get; set; }
[JsonProperty("id")]
public string id { get; set; }
}
这是我用来读取接收和解析的 json 数据的 .NET c# 代码:
var formCreateUserContent = new StringContent(json, Encoding.UTF8, "application/json"); var newUserResult = newNewUserAPIMResultingData();
using (HttpClient client2 = new HttpClient())
{
using (HttpResponseMessage response = await client2.PutAsync(url, formCreateUserContent))
{
using (HttpContent content = response.Content)
{
var stringContent = await content.ReadAsStringAsync();
newUserResult = JsonConvert.DeserializeObject<NewUserAPIMResultingData>(stringContent);
}
foreach (var z in newUserResult.properties.identities)
Console.WriteLine(z);
}
}
这是我在控制台上遇到的错误:[09/06/2020 13:34:13] Executed 'TestCreateAPIMUser' [09/06/2020 13:34:13] System.Private.CoreLib:执行函数时出现异常:TestCreateAPIMUser。Newtonsoft.Json:解析值时遇到意外字符:{。路径“properties.groups”,第 13 行,位置 7。
解决方案
您需要更改以下属性的声明。因为note
andgroups
可以是组可以是其他对象然后string
和IList<string>
public object note { get; set; }
public IList<object> groups { get; set; }
推荐阅读
- next.js - 按钮没有统一的填充
- java - 杰克逊序列化不包括字段
- laravel - 使用干预/图像调整图像大小
- android - ionic cordova build android 抛出错误:项目目标不存在
- blazor - 如何在 Blazor 中使用捆绑的 js?
- redux-toolkit - 如何使用 redux-toolkit 动态更改基本 URL?
- google-chrome - Chrome 截断长 URL
- flutter - 如果使用 sliver 应用栏,如何设置状态栏颜色
- c# - 多个可为空的 Guid,其中只有一个可以有值
- serialization - django rest framework serializer.save() 内部错误