【发布时间】:2015-06-05 01:05:05
【问题描述】:
我有以下代码。使用 JSON.NET 可以正常工作,我可以将字符串反序列化为 CustomMaker 对象。使用 ServiceStack.Text 我得到空值。我试过做 { get;放;并删除和添加构造函数。
使用 JSON.NET,它的工作原理就像 JsonConvert.DeserializeObject(xString);
知道为什么这不适用于 ServiceStack.Text 吗?
static void Main(string[] args) {
string xString = "{\"error\":\"Invalid token 1 #556264\"}";
Console.WriteLine(xString);
CustomMaker xSLBM = TypeSerializer.DeserializeFromString<CustomMaker>(xString);
Console.WriteLine(string.IsNullOrWhiteSpace(xSLBM.error) ? "error is null" : xSLBM.error);
Console.ReadLine();
}
[Serializable]
public class CustomMaker {
public int UserID;
public String error;
public CustomMaker() { }
}
编辑:这段代码也产生空值:
static void Main(string[] args) {
JsConfig.IncludePublicFields = true;
string xString = "{\"error\":\"Invalid token 1 #556264\"}";
Console.WriteLine(xString);
CustomMaker xSLBM = TypeSerializer.DeserializeFromString<CustomMaker>(xString);
Console.WriteLine(string.IsNullOrWhiteSpace(xSLBM.error) ? "error is null" : xSLBM.error);
Console.ReadLine();
}
[Serializable]
public class CustomMaker {
public CustomMaker() { }
public int UserID { get; set; }
public String error { get; set; }
}
【问题讨论】:
标签: c# .net json servicestack