解析反序列化的Json数据失败-c#

问题描述 投票:0回答:2

我有要解析的json data,但是遇到解析错误,请问能否说明一下?

这是我想在收到时解析的Json数据:

{
      "id": "/subscriptions/xxxx",
      "type": "Microsoft.ApiManagement/service/users",
      "name": "bbbbb",
      "properties": {
        "firstName": "aaaa",
        "lastName": "cccc",
        "email": "[email protected]",
        "state": "active",
        "registrationDate": "somedate",
        "note": null,
        "groups": [],
        "identities": [
          {
            "provider": "Basic",
            "id": "[email protected]"
          }
        ]
      }
    }

这些是我创建的将数据反序列化为的类:

 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 = new NewUserAPIMResultingData();

            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]执行了'TestCreateAPIMUser'[09/06/2020 13:34:13] System.Private.CoreLib:执行函数TestCreateAPIMUser时发生异常。 Newtonsoft.Json:解析值{时遇到意外字符。路径“ properties.groups”,第13行,位置7。

c# .net json deserialization
2个回答
0
投票

您需要更改以下属性的声明。由于notegroups可以是组,因此可以是其他对象,因此stringIList<string>

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

0
投票

可能与源json的编码有关?您可以使用以下测试代码来验证您的C#类定义是否正确...

    [Test]
    public void Test()
    {
        const string json = @"{
          ""id"": ""/subscriptions/xxxx"",
          ""type"": ""Microsoft.ApiManagement/service/users"",
          ""name"": ""bbbbb"",
          ""properties"": {
            ""firstName"": ""aaaa"",
            ""lastName"": ""cccc"",
            ""email"": ""[email protected]"",
            ""state"": ""active"",
            ""registrationDate"": ""somedate"",
            ""note"": null,
            ""groups"": [],
            ""identities"": [
              {
                ""provider"": ""Basic"",
                ""id"": ""[email protected]""
              }
            ]
          }
        }";

        var result = JsonConvert.DeserializeObject<NewUserAPIMResultingData>(json);

        Assert.IsNotNull(result);
        Assert.IsTrue(result.properties.identities.Count() == 1);
    }

我复制了类并通过了测试,唯一的区别是我将json粘贴为常量,因此Visual Studio自动正确地对其进行编码。

© www.soinside.com 2019 - 2024. All rights reserved.