簡體   English   中英

解析反序列化的 Json 數據失敗 - c#

[英]Parsing deserialized Json data Fails - 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:執行 function 時出現異常:TestCreateAPIMUser。 Newtonsoft.Json:解析值時遇到意外字符:{。 路徑“properties.groups”,第 13 行,position 7。

您需要更改以下屬性的聲明。 因為notegroups可以是組可以是其他 object 然后是stringIList<string>

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

可能是源 json 的編碼問題? 您可以使用以下測試代碼驗證您的 C# class 定義是否正常...

    [Test]
    public void Test()
    {
        const string 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""
              }
            ]
          }
        }";

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

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

我復制了類並通過了測試,唯一的區別是我將 json 粘貼為常量,因此 Visual Studio 自動對其進行了正確編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM