【问题标题】:Deserialize Json with an unnamed object collection使用未命名的对象集合反序列化 Json
【发布时间】: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


    【解决方案1】:

    结帐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
        }
      }
    }
    

    生成的 QuickType 代码:

    // 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 }
                },
            };
        }
    }
    

    【讨论】:

    • 谢谢,但这不是我的选择。我收到的 JSON 字符串非常大,我只发布了大约 5% 的文件大小,并且大部分都具有相同的布局,只是其他对象名称。另外我不知道布局是否始终相同。但是,当适用于其他时间时,我会记住这一点。
    • @Rakker 那么您只是发布了错误的 JSON 字符串。只要它是一个有效的 JSON 字符串,那么 QuickType 应该能够为您生成模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2016-03-26
    • 2020-02-24
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多