5

我正在尝试使用库ServiceStack.Text序列化对象。这有效

using System.Dynamic;
using ServiceStack.Text;
var x = new {Value= 10, Product = "Apples"};                
Console.WriteLine(JsonSerializer.SerializeToString(x));

我明白了,正如我所料

{"Value":10,"Product":"Apples"}

然而

dynamic x = new ExpandoObject();
x.Value = 100;
x.Product = "Apples";
Console.WriteLine(JsonSerializer.SerializeToString(x));

我大吃一惊

[{"Key":"Value","Value":100},{"Key":"Product","Value":"Apples"}]

为什么!这是怎么回事?

其次我怎样才能得到我想要的?

4

1 回答 1

5

ExpandoObject实现IConnection<KeyValuePair>IEnumerable<KeyValuePair>

public sealed class ExpandoObject :
    IDynamicMetaObjectProvider,
    IDictionary<string, object>,
    ICollection<KeyValuePair<string, object>>,
    IEnumerable<KeyValuePair<string, object>>,
    IEnumerable, INotifyPropertyChanged

我的猜测是,在内部,ServiceStack 序列化程序将ExpandoObject视为IEnumerable<KeyValuePair>,因此它序列化为键/值对的 JSON 数组。


这与您的第一个(工作)代码片段不同,因为 .NET 实际上为您的数据构建了一个真实的(匿名)类,基本上它使:

public class SomeNameTheCompilerMakesUp {
    internal int Value { get; set; }
    internal string Product { get; set; }
}

自动为您服务,因此当它被发送到序列化程序时,它正在使用具有真实属性的真实类,而ExpandoObject实际上是由object[]内部支持的。


在旁注中,微软的System.Web.Helpers.Json行为方式相同。该测试通过:

    [TestMethod]
    public void ExpandoObjectSerializesToJsonArray()
    {
        dynamic anonType = new { Value = 10, Product = "Apples" };

        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        var anonResult = System.Web.Helpers.Json.Encode(anonType);
        var expandoResult = System.Web.Helpers.Json.Encode(expando);

        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", anonResult);
        Assert.AreEqual("[{\"Key\":\"Value\",\"Value\":10},{\"Key\":\"Product\",\"Value\":\"Apples\"}]", expandoResult);
    }

最后一次编辑:

您可以通过将您的ExpandoObject变成Dictionary<string, object>. 这段代码需要注意的是,它将数据复制到字典中,因此您在内存中有 2 个副本(或略少于,因为从技术上讲,字符串可能被保留)。

    [TestMethod]
    public void TestMethod1()
    {
        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        // copy expando properties to dictionary
        var dictionary = ((ExpandoObject)expando).ToDictionary(x => x.Key, x => x.Value);

        var expandoResult    = System.Web.Helpers.Json.Encode(expando);
        var dictionaryResult = System.Web.Helpers.Json.Encode(dictionary);

        Assert.AreEqual("[{\"Key\":\"Value\",\"Value\":10},{\"Key\":\"Product\",\"Value\":\"Apples\"}]", expandoResult);
        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", dictionaryResult);
    }

虽然,对于以后遇到这种情况并且实际上正在使用的任何人,System.Web.Helpers.Json最好的办法就是将你ExpandoObject的包装成DynamicJsonObject这样:

    [TestMethod]
    public void TestMethod1()
    {
        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        var dictionaryResult = System.Web.Helpers.Json.Encode(new DynamicJsonObject(expando));

        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", dictionaryResult);
    }

Ans 一旦我解决了这个问题,我在这里发现了一个类似的问题:How to flatten an ExpandoObject returned via JsonResult in asp.net mvc?

于 2012-07-03T14:55:57.133 回答