对于'动态'实例的嵌套树,ServiceStack.Text.JsonObject.Parse与NewtonSoft.Json.Linq.JObject.Parse有什么关系?

时间:2012-11-13 18:18:54

标签: json json.net servicestack

我想尝试ServiceStack的json解析,但我已经想出了如何通过Newtonsoft做我需要的东西。可以通过ServiceStack完成同样的事情吗?

我已尝试使用已注释掉的代码但它提供了异常,请参阅下面的异常详细信息。

谢谢!

约什

    [Test]
    public void TranslateFromGitHubToCommitMessage()
    {
        const string json =
@"
{
'commits': 
[
    {
      'author': {
        'email': 'dev@null.org',
        'name': 'The Null Developer'
      },
      'message': 'okay i give in'
    },
    {
      'author': {
        'email': 'author@github.com',
        'name': 'Doc U. Mentation'
      },
      'message': 'Updating the docs, that\'s my job'
    },
    {
      'author': {
        'email': 'author@github.com',
        'name': 'Doc U. Mentation'
      },
      'message': 'Oops, typos'
    }
]
}
";
        dynamic root = JObject.Parse(json);
        //dynamic root = ServiceStack.Text.JsonSerializer.DeserializeFromString<JsonObject>(json);
        //dynamic root = ServiceStack.Text.JsonObject.Parse(json);

        var summaries = new List<string>();

        foreach (var commit in root.commits)
        {
            var author = commit.author;
            var message = commit.message;
            summaries.Add(string.Format("{0} <{1}>: {2}", author.name, author.email, message));
        }            

        const string expected1 = "The Null Developer <dev@null.org>: okay i give in";
        const string expected2 = "Doc U. Mentation <author@github.com>: Updating the docs, that's my job";
        const string expected3 = "Doc U. Mentation <author@github.com>: Oops, typos";

        Assert.AreEqual(3, summaries.Count);
        Assert.AreEqual(expected1, summaries[0]);
        Assert.AreEqual(expected2, summaries[1]);
        Assert.AreEqual(expected3, summaries[2]);
    }

例外明细

使用第一个注释掉的行时:

dynamic root = ServiceStack.Text.JsonSerializer.DeserializeFromString<JsonObject>(json);

调用方法时会发生此异常。

的NullReferenceException:

at ServiceStack.Text.Common.DeserializeListWithElements`2.ParseGenericList(String value, Type      createListType, ParseStringDelegate parseFn)
at ServiceStack.Text.Common.DeserializeEnumerable`2.<>c__DisplayClass3.<GetParseFn>b__0(String value)
at ServiceStack.Text.Common.DeserializeSpecializedCollections`2.<>c__DisplayClass7.  <GetGenericEnumerableParseFn>b__6(String x)
at ServiceStack.Text.Json.JsonReader`1.Parse(String value)
at ServiceStack.Text.JsonSerializer.DeserializeFromString[T](String value)
at GitHubCommitAttemptTranslator.Tests.GitHubCommitAttemptTranslatorTests.TranslateFromGitHubToCommitMessage()

而且,第二个:

dynamic root = ServiceStack.Text.JsonObject.Parse(json);

var summaries = new List<string>();

foreach (var commit in root.commits) // <-- Happens here

'ServiceStack.Text.JsonObject'不包含'commits'的定义

注意:如果我使用第一行的代码,则'string'不包含'提交'的定义,但是将类型更改为或改为

at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at GitHubCommitAttemptTranslator.Tests.GitHubCommitAttemptTranslatorTests.TranslateFromGitHubToCommitMessage()

使用.NET 4.0 ServiceStack中的DynamicJson

参考mythz的评论: This test case有效,但如果我修改如下:

var dog = new { Name = "Spot", Parts = new { Part1 = "black", Part2 = "gray" }, Arr = new [] { "one", "two", "three"} };
var json = DynamicJson.Serialize(dog);
var deserialized = DynamicJson.Deserialize(json);

然后,deserialized.Name和Parts很好,但Arr的类型为string。

此外:

如果我使用'引号,它似乎不起作用。这是正常的吗? json2工作(Arr仍然是一个字符串的程度),但json3根本不起作用。它只是返回

Immediate Window:

deserialized = DynamicJson.Deserialize(json3);
{}
base {System.Dynamic.DynamicObject}: {}
_hash: Count = 1

----- code: -----
        var json2 =
            @"
        {
          ""Name"": ""Spot"",
          ""Parts"": {
            ""Part1"": ""black"",
            ""Part2"": ""gray""
          },
          ""Arr"": [
            ""one"",
            ""two"",
            ""three""
          ]
        }";

        var json3 =
            @"
        {
          'Name': 'Spot',
          'Parts': {
            'Part1': 'black',
            'Part2': 'gray'
          },
          'Arr': [
            'one',
            'two',
            'three'
          ]
        }";

        var deserialized = DynamicJson.Deserialize(json1);            

1 个答案:

答案 0 :(得分:3)

ServiceStack的JSON Serializer也支持动态解析,请参阅如何在wiki页面的Dynamic JSON section中解析GitHub的JSON的示例。