首页 > 解决方案 > 解析反序列化的 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。

标签: c#.netjsondeserialization

解决方案


您需要更改以下属性的声明。因为noteandgroups可以是组可以是其他对象然后stringIList<string>

public object note { get; set; }        
public IList<object> groups { get; set; }

推荐阅读