如何在Xamarin形式的视图模型中反序列化对象

时间:2019-10-18 05:18:05

标签: xamarin-forms-4

我需要反序列化对象,因为我尝试了一些已注释但不起作用的方法

   public async Task FetchData()
    {
        using (HttpClient hc = new HttpClient())
        {
            MessageList msgList = null;
            try
            {
                IsBusy = true;
                var jsonString = await hc.GetStringAsync(url);
                //string json = await jsonString.Content.ReadAsStringAsync();
                //msgList = JsonConvert.DeserializeObject<MessageList>(jsonString);
                //foreach (var user in msgList) { 
                    //string IsRead = obj.hasBeenRead;
                    //if(IsRead== "true")
                    //{
                    //    obj.IsRedDotVisible = false;
                    //}
                    //else
                    //{
                    //    obj.IsRedDotVisible = true;
                    //}

                    //OpenData.Add(user);
                    OpenData = MessageList.FromJson(jsonString);

               // }
                // IsEmpty = OpenData.Count == 0;
            }

 and i have Message list is like this 

public partial class MessageList
{ 
    [JsonProperty("threadId")]
    public int threadId { get; set; }
    [JsonProperty("dealId")]
    public int dealId { get; set; }
    [JsonProperty("topic")]
    public string topic { get; set; }
    [JsonProperty("lastPostDate")]
    public Nullable<DateTime> lastPostDate { get; set; }
    [JsonProperty("lastPostBy")]
    public string lastPostBy { get; set; }
    [JsonProperty("hasBeenRead")]
    public string hasBeenRead { get; set; }
    [JsonProperty("lastMessage")]
    public string lastMessage { get; set; }
    [JsonProperty("partyName")]
    public string partyName { get; set; }
    public bool IsRedDotVisible { get; set; }
}
public partial class MessageList
{
    public static ObservableCollection<MessageList> FromJson(string json) => JsonConvert.DeserializeObject<ObservableCollection<MessageList>>(json);
}
}

并且我能够在列表中显示数据,但是我想在需要添加一些条件的情况下反序列化对象,所以该怎么做,任何人都可以帮我解决这个问题。

0 个答案:

没有答案