【发布时间】:2018-04-06 07:50:33
【问题描述】:
我一直在寻找将我的 Json 反序列化为 C# 类的任何解决方案。我已经这样做了很多次,但现在我遇到了一种我不知道如何处理的特定格式。
{
"32": {
"name": "Basic",
"data": {
"value": null,
"type": "empty",
"supported": {
"value": true,
"type": "bool",
"invalidateTime": 1520421448,
"updateTime": 1520421449
},
"version": {
"value": 1,
"type": "int",
"invalidateTime": 1520421448,
"updateTime": 1520421449
},
"security": {
"value": false,
"type": "bool",
"invalidateTime": 1520421448,
"updateTime": 1520421449
},
"invalidateTime": 1520421448,
"updateTime": 1520421449
}
}
如您所见,“value”、“type”、“invalidateTime”和“updateTime”在它们的容器中是重复的。
我创建了一个类“DataProperty”
public class DataProperty
{
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("invalidateTime")]
public string InvalidateTime { get; set; }
[JsonProperty("updateTime")]
public string UpdateTime { get; set; }
}
还有一个“DataClass”类 => 我知道,需要重命名
public class DataClass
{
[JsonProperty]
public DataProperty Data { get; set; }
[JsonProperty]
public Dictionary<string,DataProperty> SubProperties { get; set; }
}
我从“DataProperty”开始测试自下而上的反序列化,并在反序列化 json 中的“data”实体时遇到问题,因此我还没有开发父类,但提交了 json 的更大部分以供概述。当字典部分不在单独的命名容器中并且我不知道如何处理时,我的问题就开始了。我创建了一个属性“SubProperties”,我并不为此感到自豪,但不知道该怎么做。
这里,如果有人想测试,我将复制一些行。
string testdata = "{\"data\": {\r\n \"value\": null,\r\n \"type\": \"empty\",\r\n \"supported\": {\r\n \"value\": true,\r\n \"type\": \"bool\",\r\n \"invalidateTime\": 1520420777,\r\n \"updateTime\": 1520420746\r\n },\r\n \"version\": {\r\n \"value\": 1,\r\n \"type\": \"int\",\r\n \"invalidateTime\": 1520420777,\r\n \"updateTime\": 1520420746\r\n },\r\n \"security\": {\r\n \"value\": false,\r\n \"type\": \"bool\",\r\n \"invalidateTime\": 1520420745,\r\n \"updateTime\": 1520420746\r\n },\r\n \"interviewDone\": {\r\n \"value\": true,\r\n \"type\": \"bool\",\r\n \"invalidateTime\": 1520420745,\r\n \"updateTime\": 1520420777\r\n },\r\n \"interviewCounter\": {\r\n \"value\": 9,\r\n \"type\": \"int\",\r\n \"invalidateTime\": 1520420745,\r\n \"updateTime\": 1520420749\r\n },\r\n \"level\": {\r\n \"value\": 255,\r\n \"type\": \"int\",\r\n \"invalidateTime\": 1520420776,\r\n \"updateTime\": 1520420777\r\n },\r\n \"invalidateTime\": 1520420777,\r\n \"updateTime\": 1520420746\r\n}}"
DataClass dat = JsonConvert.DeserializeObject<DataClass>(testdata);
提前感谢您的宝贵时间。
【问题讨论】:
标签: c# json serialization