如何使用 DataContractJsonSerializer 反序列化嵌套的 json?
How can I Deserialize nested json with DataContractJsonSerializer?
我正在尝试像这样反序列化 json:
{
"version": 3070,
"settings": [
[
"settingInteger",
0,
"1513486800",
{
"param": "A"
}
],
[
"settingBool",
1,
"false",
{
"param": "B"
}
],
[
"settingString",
2,
"somedata",
{
"param" : "C"
}
]
]
}
所以我尝试用 DataContraceJsonSerializer
:
反序列化到这个 class
[DataContract]
public class SettingSet
{
[DataMember(Name = "version")]
public int Version { get; set; }
[DataMember(Name = "settings)]
public Setting[] Settings { get; set; }
}
[DataContract]
public class Setting
{
[DataMember(Order = 0)]
public string OptionName { get; set; }
[DataMember(Order = 1)]
public SettingType Type { get; set; }
[DataMember(Order = 2)]
public string Value { get; set; }
[DataMember(Order = 3)]
public Dictionary<string, string> Parameters { get; set; }
}
但我只能得到 Version
和空数组 Settings
。
如果我使用 object[][]
而不是 Setting[]
,我可以获得一些数据,但是 param
仍然无法访问。
有什么方法可以解决这个问题,还是我应该使用其他 JSON 库?
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SettingSet settingSet = serializer.Deserialize<SettingSet>(jSon);
我正在尝试像这样反序列化 json:
{
"version": 3070,
"settings": [
[
"settingInteger",
0,
"1513486800",
{
"param": "A"
}
],
[
"settingBool",
1,
"false",
{
"param": "B"
}
],
[
"settingString",
2,
"somedata",
{
"param" : "C"
}
]
]
}
所以我尝试用 DataContraceJsonSerializer
:
[DataContract]
public class SettingSet
{
[DataMember(Name = "version")]
public int Version { get; set; }
[DataMember(Name = "settings)]
public Setting[] Settings { get; set; }
}
[DataContract]
public class Setting
{
[DataMember(Order = 0)]
public string OptionName { get; set; }
[DataMember(Order = 1)]
public SettingType Type { get; set; }
[DataMember(Order = 2)]
public string Value { get; set; }
[DataMember(Order = 3)]
public Dictionary<string, string> Parameters { get; set; }
}
但我只能得到 Version
和空数组 Settings
。
如果我使用 object[][]
而不是 Setting[]
,我可以获得一些数据,但是 param
仍然无法访问。
有什么方法可以解决这个问题,还是我应该使用其他 JSON 库?
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SettingSet settingSet = serializer.Deserialize<SettingSet>(jSon);