以属性为键将对象列表序列化为单个JSON对象

时间:2018-10-19 09:08:22

标签: c# json serialization json.net

是否可以使用Newtonsoft Json.NET将要序列化(通过集合)的类的属性之一用作该类的键?

例如,在C#中采用以下类

dict = {'A': 71.07884,
    'B': 110
    'C': 103.14484,
    'D': 115.08864,
    'E': 129.11552,
    'F': 147.1766,
   }

name = "ABC"

for character in name:
    for key, value in dict.items():
        if key == character:
            print(value)

如果我运行以下命令序列化集合...

public class MyClass
{
  public string UniqueId { get; set; }
  public string Value { get; set; }
}
public class MyCollection : List<MyClass>
{ }

这将导致以下JSON,以及包含每个var col = new MyCollection() { new MyClass { UniqueId = "First", Value = "This") }, new MyClass { UniqueId = "Second", Value = "That") } }; string json = JsonConvert.SerializeObject(col); 的数组。

MyClass

有什么方法可以迫使Newtonsoft创建以下对象,而不是创建一个对象(而不是数组),并以[ { "UniqueId": "First", "Value": "This" }, { "UniqueId": "Second", "Value": "That" } ] 属性引用的每个类为键? < / p>

UniqueId

或者如果更容易...

{
  "First":
  {
    "Value": "This"
  },
  "Second":
  {
    "Value": "That"
  }
}

我知道使用{ "First": { "UniqueId": "First", "Value": "This" }, "Second": { "UniqueId": "Second", "Value": "That" } } 会得到我想要的结果,因为该属性使用与键相同的值...但是,这是无序,我需要Dictionary<>才能正常使用。

2 个答案:

答案 0 :(得分:1)

可以使用OrderedDictionary保持添加项目的顺序,也可以使用SortedDictionary按键对项目进行排序

给出以下模型:

public class MyClass
{
    public string UniqueId { get; set; }
    public string Value { get; set; }
}

以及以下实例:

var first = new MyClass {UniqueId = "First", Value = "This"};
var second = new MyClass {UniqueId = "Second", Value = "That"};
var third = new MyClass {UniqueId = "Third", Value = "Foo"};
var fourth = new MyClass {UniqueId = "Fourth", Value = "Bar"};

使用OrderedDictionary

var dictionary = new OrderedDictionary()
{
    { first.UniqueId, first },
    { second.UniqueId, second },
    { third.UniqueId, first },
    { fourth.UniqueId, first },
};

string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

这将保持添加项目的顺序。输出json如下:

{
  "First": {
    "UniqueId": "First",
    "Value": "This"
  },
  "Second": {
    "UniqueId": "Second",
    "Value": "That"
  },
  "Third": {
    "UniqueId": "First",
    "Value": "This"
  },
  "Fourth": {
    "UniqueId": "First",
    "Value": "This"
  }
}

使用SortedDictionary

var first = new MyClass {UniqueId = "First", Value = "This"};
var second = new MyClass {UniqueId = "Second", Value = "That"};
var third = new MyClass {UniqueId = "Third", Value = "Foo"};
var fourth = new MyClass {UniqueId = "Fourth", Value = "Bar"};

var dictionary = new SortedDictionary<string, MyClass>
{
    { first.UniqueId, first },
    { second.UniqueId, second },
    { third.UniqueId, first },
    { fourth.UniqueId, first },
};

string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

这将按键对项目进行排序(Fourth是第二项,而不是第四项)。输出json如下:

{
  "First": {
    "UniqueId": "First",
    "Value": "This"
  },
  "Fourth": {
    "UniqueId": "First",
    "Value": "This"
  },
  "Second": {
    "UniqueId": "Second",
    "Value": "That"
  },
  "Third": {
    "UniqueId": "First",
    "Value": "This"
  }
}

答案 1 :(得分:0)

OrderedDictionary的强类型替代方法-将List<KeyValuePair<key, value>>Dictionary一起使用。

因此,给定以下模型:

public class MyClass
{
    public string UniqueId { get; set; }
    public string Value { get; set; }
}

以及以下实例:

var first = new MyClass {UniqueId = "First", Value = "This"};
var second = new MyClass {UniqueId = "Second", Value = "That"};
var third = new MyClass {UniqueId = "Third", Value = "Foo"};
var fourth = new MyClass {UniqueId = "Fourth", Value = "Bar"};

不是像这样创建和初始化Dictionary

var dictionary = new Dictionary<string, MyClass>
{
    { first.UniqueId, first },
    { second.UniqueId, second },
    { third.UniqueId, first },
    { fourth.UniqueId, first },
};

我们可以创建并初始化KeyValuePair列表,然后创建Dictionary

var pairs = new List<KeyValuePair<string, MyClass>>
{
    new KeyValuePair<string, MyClass>(first.UniqueId, first),
    new KeyValuePair<string, MyClass>(second.UniqueId, second),
    new KeyValuePair<string, MyClass>(third.UniqueId, third),
    new KeyValuePair<string, MyClass>(fourth.UniqueId, fourth),
};

var dictionary = new Dictionary<string, MyClass>(pairs);

这将保留添加到KeyValuePair列表中的项目的顺序。序列化Dictionary

string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

输出json如下:

{
  "First": {
    "UniqueId": "First",
    "Value": "This"
  },
  "Second": {
    "UniqueId": "Second",
    "Value": "That"
  },
  "Third": {
    "UniqueId": "Third",
    "Value": "Foo"
  },
  "Fourth": {
    "UniqueId": "Fourth",
    "Value": "Bar"
  }
}
相关问题