使用未命名的对象集合反序列化Json

时间:2018-04-06 07:50:33

标签: c# json serialization

我一直在寻找将我的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);

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

结帐json2csharp,因为它有Generate with QuickType选项。这对那些不想手动为其JSON字符串生成模型的人非常有用。

JSON:

{
  "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
    }
  }
}

生成的快速类型代码:

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var welcome = Welcome.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Welcome
    {
        [JsonProperty("32")]
        public The32 The32 { get; set; }
    }

    public partial class The32
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("data")]
        public Data Data { get; set; }
    }

    public partial class Data
    {
        [JsonProperty("value")]
        public object Value { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("supported")]
        public Security Supported { get; set; }

        [JsonProperty("version")]
        public Version Version { get; set; }

        [JsonProperty("security")]
        public Security Security { get; set; }

        [JsonProperty("invalidateTime")]
        public long InvalidateTime { get; set; }

        [JsonProperty("updateTime")]
        public long UpdateTime { get; set; }
    }

    public partial class Security
    {
        [JsonProperty("value")]
        public bool Value { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("invalidateTime")]
        public long InvalidateTime { get; set; }

        [JsonProperty("updateTime")]
        public long UpdateTime { get; set; }
    }

    public partial class Version
    {
        [JsonProperty("value")]
        public long Value { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("invalidateTime")]
        public long InvalidateTime { get; set; }

        [JsonProperty("updateTime")]
        public long UpdateTime { get; set; }
    }

    public partial class Welcome
    {
        public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = { 
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}