改变ServiceStack.Text的输出JSON解串器

问题描述:

我目前使用Newtonsoft.json nuget包,但我想找到一个更快的选择。​​似乎解析它,但它以我期待的不同格式返回JSON。无论如何要改变这个返回对象的格式来匹配我所期望的吗?改变ServiceStack.Text的输出JSON解串器

的问题是是反序列化后,response.fullName回报“乔伊酷”如预期Newtonsoft.json但​​将返回null因为该格式是不同的。

我可以改变​​的输出格式,以便它符合我的期望吗?我想打电话给response.fullName,并获得“joey酷”。

下面是使用我的代码​​组件:

T response = a_response.Content.FromJson<T>(); 

这里使用Newtonsoft.json我的代码:

T response = JsonConvert.DeserializeObject<T>(a_response.Content); 

下面是JSON文本的外观作为输出:

{ “用户id”: “fc7b4c4e0b6660c7daf711b1d17e0039”, “EMAILADDRESS”: “[email protected]”, “全名”: “袋鼠酷”, “ACCOUNTTYPE”: “个人”, “createdDate “:1440104822411, ”phoneNumber的“: ”15555555555“, ”时区“: ”美国/洛杉矶“, ”照片“:” https://stringify.s3.amazonaws.com/users/fc7b4c4e0b6660c7daf711b1d17e0039-profile.jpg”, “名”: “默认”, “类型”: “API” }

下面是调试器如何显示Newtonsoft.json对象: enter image description here

下面是你的​​JSON解串器如何显示响应对象: enter image description here

---- ----编辑试图 从4.0.62和的NuGet它给了我一个例外。

消息:“ServiceStack.StringExtensions”的类型初始值设定项引发异常。对象引用不设置为对象的情况下,在ServiceStack.StringExtensions..cctor()[0x00017]在:0

enter image description here

----- ----- EDIT

URL to a file containing the JSON class

Here's a video demonstrating the usage differences and the strange output

您可以尝试使用最新V4 ServiceStack的。文字which is now free from v4.0.62+并且是available in all Xamarin platforms

从v3开始,它有很多改进,所以如果这个行为是由于v3中的bug造成的,现在可能已经修复了。

编辑:

你想序列化这个类是无效的,无论是ServiceStack词典序列化或POCO类具有公共属性,它不都做,比如:

[JsonObject (MemberSerialization.OptIn)] 
public class UserDataDict : Dictionary<string, object> 
{ 
    [JsonProperty] 
    public string userID { get; set; } 
    [JsonProperty] 
    public string emailAddress { get; set; } 
    [JsonProperty] 
    public string fullName { get; set; } 
    [JsonProperty] 
    public string accountType { get; set; } 
    [JsonProperty] 
    public string units { get; set; } 
    [JsonProperty] 
    public string unitsDistance { get; set; } 
    [JsonProperty] 
    public string newsletterSub { get; set; } 

    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string location { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string phoneNumber { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string address { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string photo { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string createdDate { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string verifyURL { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] 
    public string timezone { get; set; } 
    [JsonProperty (NullValueHandling = NullValueHandling.Include)] 
    public APIManifestDict apiManifest { get; set; } 
} 

我会删除继承字典,因为它有更多的互操作性让你的类包含一个字典,然后继承它。此外,您的[JsonProperty]属性JSON.NET具体和有其他序列化没有任何影响,所以我想你的类改写为:

public class UserData 
{ 
    public string userID { get; set; } 
    public string emailAddress { get; set; } 
    public string fullName { get; set; } 
    public string accountType { get; set; } 
    public string units { get; set; } 
    public string unitsDistance { get; set; } 
    public string newsletterSub { get; set; } 

    public string location { get; set; } 
    public string phoneNumber { get; set; } 
    public string address { get; set; } 
    public string photo { get; set; } 
    public string createdDate { get; set; } 
    public string verifyURL { get; set; } 
    public string timezone { get; set; } 
    public APIManifestDict apiManifest { get; set; } 

    public Dictionary<string,string> Metadata { get; set; } 
} 

如果要包括空你可以指定它:

JsConfig.IncludeNullValues = true; 

但是,如果您的应用不包含在JSON有效内容中,那么您的属性自然为空,所以我建议您不要依赖存在空值的应用。包括空值更加脆弱,必须满足多空的定义,禁止版本控制并且使有效载荷膨胀。

+0

当我尝试从NuGet使用4.0.62时,出现异常。这是一个Xamarin问题吗?我附上了上述例外的更多细节。 消息:“ServiceStack.StringExtensions”的类型初始值设定项引发异常。 Object Ref未设置为ServiceStack.StringExtensions..cctor()中的对象实例中的[0x00017]:0 – LampShade

+0

您需要[初始化客户端](https://github.com/ServiceStackApps/HelloMobile/ blob/master/README.md#initializing-pcl-client)如果你还没有 – mythz

+0

所以我得到了这个工作,不得不使用ServiceStack.Client而不是默认的。但我仍然有同样的问题。 JSON显示不正确。它以与Xamarin组件相同的格式输出。 – LampShade