我想尝试ServiceStack的json解析,但我已经找到了如何通过Newtonsoft完成我需要的东西。同样的事情可以通过ServiceStack完成吗?
我已经尝试过注释掉的代码,但它给出了异常,请参阅下面的异常详细信息。
谢谢!
Josh
[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"不包含"提交"的定义
注意:如果我使用第一行的代码,但将类型更改为或而不是,则消息为"string"不包含"commits"的定义
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的评论:这个测试用例是有效的,但如果我像下面这样修改它:
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);
然后,反序列化。Name和Parts可以,但Arr属于字符串类型。
还有:
如果我使用引号,它似乎不起作用。这正常吗?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);
ServiceStack的JSON序列化程序也支持动态解析,请参阅wiki页面的动态JSON部分中有关如何解析GitHub的JSON的示例。