简体   繁体   中英

Parsing deserialized Json data Fails - c#

I have this json data to parse but I get parsing error, can some please shed some light?

This is the Json data I'd like to parse when received:

{
      "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"
          }
        ]
      }
    }

These are the classes I've created to deserialize data into:

 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; }
        }

This is the .NET c# code i'm using to read received and parsed json data:

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);
                }
            }

This is the error I get on console: [09/06/2020 13:34:13] Executed 'TestCreateAPIMUser' [09/06/2020 13:34:13] System.Private.CoreLib: Exception while executing function: TestCreateAPIMUser. Newtonsoft.Json: Unexpected character encountered while parsing value: {. Path 'properties.groups', line 13, position 7.

you need to change the declaration for the below properties. Because note and groups can be groups can be other object then string and IList<string>

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

Possibly an issue with the encoding of the source json? You can verify that your C# class definitions are OK using the following test code...

    [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);
    }

I copied the classes and the tests passed, the only difference is that I pasted the json as a constant so Visual Studio automatically encoded it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM