首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Json.Net中序列化类名

在Json.Net中序列化类名
EN

Stack Overflow用户
提问于 2016-06-16 22:42:05
回答 1查看 1.3K关注 0票数 0

我有以下C#代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public class Program
{
    private static readonly JsonSerializerSettings prettyJson = new JsonSerializerSettings()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        Formatting = Formatting.Indented
    };
    public class dialup {
        public Dictionary<string,uint> speeds;
        public string phonenumber;
    }
    public class Ethernet {
        public string speed;
    }
    public class ipv4 {
        public bool somecapability;
    }

    public class SiteData {
        public string SiteName;

        [JsonExtensionData]
        public Dictionary<string, object> ConnectionTypes;
    }
    public static void Main() 
    {   
        var data = new SiteData()
        {
            SiteName = "Foo",
            ConnectionTypes = new Dictionary<string, object>() 
            {
                { "1",  new dialup() { speeds=new Dictionary<string,uint>() {{"1",9600},{"2",115200}}, phonenumber = "0118 999 881 999 119 725 ... 3" } },
                { "2",  new Ethernet() { speed = "1000" } },
                {"3", new ipv4() { somecapability=true}}
            }
        };
        var json = JsonConvert.SerializeObject(data, prettyJson);   
        Console.WriteLine(json);
    }
}

这将导致以下JSON:

代码语言:javascript
复制
{
  "siteName": "Foo",
  "1": {
    "speeds": {
      "1": 9600,
      "2": 115200
    },
    "phonenumber": "0118 999 881 999 119 725 ... 3"
  },
  "2": {
    "speed": "1000"
  },
  "3": {
    "somecapability": true
  }
}

在JSON中我需要的是:

代码语言:javascript
复制
{
  "siteName": "Foo",
  "1": {
    "dialup":{
    "speeds": {
      "1": 9600,
      "2": 115200
    },
    "phonenumber": "0118 999 881 999 119 725 ... 3"
    }
  },
  "2": {
    "Ethernet":{
    "speed": "1000"
    }
  },
  "3": {
    "ipv4":{
    "somecapability": true
    }
  }
}

我如何使用Json.NET来完成这个任务呢?Json.NET可以很好地反序列化它,但是我一直在寻找如何使它以同样的方式序列化。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-17 01:00:36

要实现这一点,您需要将ConnectionTypes字典中的每个值封装在另一个字典中。您可以创建一个帮助方法,以使这更容易:

代码语言:javascript
复制
private static Dictionary<string, object> WrapInDictionary(object value) 
{
    return new Dictionary<string, object>()
    {
        { value.GetType().Name, value }
    };
}

然后您可以像这样初始化您的数据:

代码语言:javascript
复制
var data = new SiteData()
{
    SiteName = "Foo",
    ConnectionTypes = new Dictionary<string, object>() 
    {
        { "1", WrapInDictionary( new dialup() { Speeds = new Dictionary<string, uint>() { {"1", 9600}, {"2", 115200} }, PhoneNumber = "0118 999 881 999 119 725 ... 3" } ) },
        { "2", WrapInDictionary( new Ethernet() { Speed = "1000" } ) },
        { "3", WrapInDictionary( new ipv4() { SomeCapability=true } ) }
    }
};

小提琴:https://dotnetfiddle.net/gGXlDo

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37870530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档