如何反序列化复杂的json字符串



1.我的json字符串如下所示:

{
"TagOptions": {
"AResouceGroupId": "rg-aekzsbuw5climmq",
"BResouceGroupId": "rg-aekztehix5f6eei",
"CResouceGroupId": "rg-aek2t7m7dcjqvhq",
"AClusterGroups": [ 0 ],
"BClusterGroups": [ 0 ],
"CClusterGroups": [ 46 ],
"DClusterGroups": [ 153, 98, 23, 3, 4, 5, 8, 9, 10, 18, 28, 99, 101, 102, 104, 105, 201 ],
"DataDeleiveryTeamProject": [
{
"GroupId": 14,
"ProjectName": "XXX-A"
},
{
"GroupId": 15,
"ProjectName": "XXX-B"
},
{
"GroupId": 46,
"ProjectName": "XXX-C"
},
{
"GroupId": 101,
"ProjectName": "XXX-D"
}
],
"DataCenterTeamProject": [
{
"GroupId": 0,
"ProjectName": "Empty"
}
],
"BazhuayuTeamProject": [
{
"GroupId": 153,
"ProjectName": "XXX-OP1"
},
{
"GroupId": 98,
"ProjectName": "XXX-OP2"
},
{
"GroupId": 23,
"ProjectName": "XXX-OP3"
},
{
"GroupId": 201,
"ProjectName": "XXX-OP4"
},
{
"GroupId": 3,
"ProjectName": "XXX-OP5"
},
{
"GroupId": 4,
"ProjectName": "XXX-OP6"
},
{
"GroupId": 8,
"ProjectName": "XXX-OP7"
},
{
"GroupId": 9,
"ProjectName": "XXX-OP8"
},
{
"GroupId": 10,
"ProjectName": "XXX-OP9"
},
{
"GroupId": 28,
"ProjectName": "XXX-OP10"
},
{
"GroupId": 102,
"ProjectName": "XXX-OP11"
},
{
"GroupId": 103,
"ProjectName": "XXX-OP12"
},
{
"GroupId": 104,
"ProjectName": "XXX-OP13"
},
{
"GroupId": 105,
"ProjectName": "XXX-OP14"
},
{
"GroupId": 18,
"ProjectName": "XXX-OP15"
},
{
"GroupId": 99,
"ProjectName": "XXX-OP16"
}
]
}
}

2.然后,我尝试将json字符串反序列化为targete类,目标类如下:

using System.Collections.Generic;
namespace Tag
{
public class TagOptions
{
public string AResouceGroupId { get; set; }
public string BResouceGroupId { get; set; }
public string CResouceGroupId { get; set; }
public int[] AClusterGroups { get; set; }
public int[] BClusterGroups { get; set; }
public int[] CClusterGroups { get; set; }
public int[] OctoparseClusterGroups { get; set; }
public List<Project> ATeamProject { get; set; }
public List<Project> BTeamProject { get; set; }
public List<Project> CTeamProject { get; set; }
}
public class Project
{
public int GroupId { get; set; }
public string ProjectName { get; set; }
}
}

方法是这样的,我使用.netframework4.5和Newtonsoft。Json

var s = jsonSerializer.Deserialize<CloudNodeTagOptions>(jsonStr);

但是结果为空。有解决问题的办法吗?

您的模型不符合您尝试使用的JSON。将您的模型更改为如下所示的示例,则Deserialize将起作用。


public class MyModel
{
public Tagoptions TagOptions { get; set; }
}
public class Tagoptions
{
public string AResouceGroupId { get; set; }
public string BResouceGroupId { get; set; }
public string CResouceGroupId { get; set; }
public int[] AClusterGroups { get; set; }
public int[] BClusterGroups { get; set; }
public int[] CClusterGroups { get; set; }
public int[] DClusterGroups { get; set; }
public Project[] DataDeleiveryTeamProject { get; set; }
public Project[] DataCenterTeamProject { get; set; }
public Project[] BazhuayuTeamProject { get; set; }
}
public class Project
{
public int GroupId { get; set; }
public string ProjectName { get; set; }
}

反序列化如下,

var s = jsonSerializer.Deserialize<MyModel>(jsonStr);

目标对象定义不正确,JSON属性必须与类属性和类型匹配。使用以下类别:

public class TargetClass
{
public TagOptions TagOptions { get; set; }
}
public class TagOptions
{
public string AResouceGroupId { get; set; }
public string BResouceGroupId { get; set; }
public string CResouceGroupId { get; set; }
public int[] AClusterGroups { get; set; }
public int[] BClusterGroups { get; set; }
[JsonProperty("DClusterGroups")]
public int[] OctoparseClusterGroups { get; set; }
[JsonProperty("DataDeleiveryTeamProject")]
public List<Project> ATeamProject { get; set; }
[JsonProperty("DataCenterTeamProject")]
public List<Project> BTeamProject { get; set; }
[JsonProperty("BazhuayuTeamProject")]
public List<Project> CTeamProject { get; set; }
}
public class Project
{
public int GroupId { get; set; }
public string ProjectName { get; set; }
}

注意,我添加了JsonProperty属性来告诉反序列化程序正确的属性名称。

然后,您可以使用以下代码来反序列化指定目标类:

var jsonObj = JsonConvert.DeserializeObject<TargetClass>(jsonStr);

最新更新