JSON具有复杂的对象,其中一些对象具有名称空间信息,我该如何在Deserialize中对此进行说明。下面的简化示例
{
"id": "123456",
"urn:test:params:scim:schemas:extension:PersonBioExtension": {
"dateOfBirth": "1900-01-01",
"gender": "MALE",
}
}
反序列化代码:
Rootobject user = JsonConvert.DeserializeObject<Rootobject>(textBox1.Text);
课程:
public class Rootobject
{
public string id { get; set; }
public TestScimSchemasExtensionBio testScimSchemasExtensionBio { get; set; }
}
public class TestScimSchemasExtensionBio
{
public string dateOfBirth { get; set; }
public string gender { get; set; }
}
答案 0 :(得分:0)
正如注释中提到的,在将属性序列化为JSON时,您可以使用JsonPropertyAttribute来更改属性的名称。
Json字符串:
{
"id": "123456",
"urn:test:params:scim:schemas:extension:PersonBioExtension": {
"dateOfBirth": "1900-01-01",
"gender": "MALE",
}
}
型号:
public class Model
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("urn:test:params:scim:schemas:extension:PersonBioExtension")]
public TestScimSchemasExtensionBio TestScimSchemasExtensionBio { get; set; }
}
public class TestScimSchemasExtensionBio
{
[JsonProperty("dateOfBirth")]
public DateTimeOffset DateOfBirth { get; set; }
[JsonProperty("gender")]
public string Gender { get; set; }
}
反序列化:
string json = @"{
""id"": ""123456"",
""urn:test:params:scim:schemas:extension:PersonBioExtension"": {
""dateOfBirth"": ""1900-01-01"",
""gender"": ""MALE"",
}
}";
Model model = JsonConvert.DeserializeObject<Model>(json);