2016-08-12 63 views
2

我目前使用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

回答

1

您可以嘗試使用最新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