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

解析反序列化Json数据失败-c#,c#,.net,json,deserialization,C#,.net,Json,Deserialization,我有这个要解析,但我得到了解析错误,能不能请一些人解释一下 这是我希望在收到Json数据时解析的Json数据: { "id": "/subscriptions/xxxx", "type": "Microsoft.ApiManagement/service/users", "name": "bbbbb", "properties": { "firstName": "aaaa", "lastName": "cccc",

我有这个要解析,但我得到了解析错误,能不能请一些人解释一下

这是我希望在收到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; }
        }
公共类NewUserAPIMResultingData
{
[JsonProperty(“id”)]
公共字符串id{get;set;}
[JsonProperty(“类型”)]
公共字符串thisType{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“属性”)]
public NewUserAPIMResultingDataProperties属性{get;set;}
}
公共类NewUserAPIMResultingDataProperties
{
[JsonProperty(“名字”)]
公共字符串userFirstName{get;set;}
[JsonProperty(“lastName”)]
公共字符串userLastName{get;set;}
[JsonProperty(“电子邮件”)]
公共字符串userEmail{get;set;}
[JsonProperty(“州”)]
公共字符串状态{get;set;}
[JsonProperty(“注册日期”)]
公共字符串注册日期{get;set;}
[JsonProperty(“注释”)]
公共字符串注释{get;set;}
[JsonProperty(“集团”)]
公共IEnumerable组{get;set;}
[JsonProperty(“身份”)]
公共IEnumerable标识{get;set;}
}
公共类NewUserAPIMResultingDataPropertyIdentity
{
[JsonProperty(“提供商”)]
公共字符串提供程序{get;set;}
[JsonProperty(“id”)]
公共字符串id{get;set;}
}
这是我用来读取接收和解析的json数据的.NET c#代码:

var formCreateUserContent=newstringcontent(json,Encoding.UTF8,“application/json”); var newUserResult=newnewuserapimresultingdata()

使用(HttpClient client2=new HttpClient())
{
使用(httpresponsemessageresponse=wait client2.PutAsync(url,formCreateUserContent))
{
使用(HttpContent=response.content)
{
var stringContent=await content.ReadAsStringAsync();
newUserResult=JsonConvert.DeserializeObject(stringContent);
}
foreach(newUserResult.properties.Identifies中的var z)
控制台写入线(z);
}
}
这是我在控制台上得到的错误:[09/06/2020 13:34:13]执行了'TestCreateAPIMUser'
[09/06/2020 13:34:13]System.Private.CoreLib:执行函数时发生异常:TestCreateAPIMUser。Newtonsoft.Json:解析值时遇到意外字符:{.Path'properties.groups',第13行,位置7。

您需要更改以下属性的声明。因为
注意
组可以是组,组可以是其他对象,然后
字符串
IList

公共对象注释{get;set;}
公共IList组{get;set;}

您需要更改以下属性的声明。因为
注意
组可以是组,组可以是其他对象,然后
字符串
IList

公共对象注释{get;set;}
公共IList组{get;set;}

源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"": ""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=@”{
“id”:“/subscriptions/xxxx”,
“”类型“”:“”Microsoft.ApiManagement/service/users“”,
“名称”:“bbbbb”,
“属性”:{
“firstName”:“aaaa”,
“lastName”:“cccc”,
“电子邮件”:someone@somewhere.xyz"",
“”状态“”:“”活动“”,
“注册日期”:“某个日期”,
“”注意“”:空,
“组”:[],
“身份”:[
{
“提供程序”:“基本”,
“id”:someone@somewhere.xyz""
}
]
}
}";
var result=JsonConvert.DeserializeObject(json);
Assert.IsNotNull(结果);
Assert.IsTrue(result.properties.identifications.Count()==1);
}

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

源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"": ""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=@”{
“id”:“/subscriptions/xxxx”,
“”类型“”:“”Microsoft.ApiManagement/service/users“”,
“名称”:“bbbbb”,
“属性”:{
“firstName”:“aaaa”,
“lastName”:“cccc”,
“电子邮件”:someone@somewhere.xyz"",
“”状态“”:“”活动“”,
“注册日期”:“某个日期”,
“”注意“”:空,
“组”:[],
“身份”:[
{
“提供程序”:“基本”,
“id”:someone@somewhere.xyz""
}
]
}
}";
var result=JsonConvert.DeserializeObject(json);
Assert.IsNotNull(结果);
Assert.IsTrue(result.properties.identifications.Count()==1);
}

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

错误是什么?我已使用解析错误更新了答案。您可以进行此更改并尝试
公共对象注释{g
    [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);
    }