简体   繁体   English

将对象数组反序列化为 C# 类在 Xamarin.Forms App 中返回 null

[英]Deserialize array of objects to c# class returns null in Xamarin.Forms App

I have this class in my App:我的应用程序中有这个类:

public class Control
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ControlDescription { get; set; }
    public string ControlExceptionText { get; set; }
    public string Color { get; set; }
    public double DataRetrieving_Frequency { get; set; }
    public double delay_Factor { get; set; }
    public Boolean Disabled { get; set; }
}

My json string is :我的 json 字符串是:

var result = 
"[
{\"id\":16,\"name\":\"trest\",\"controlDescription\":null,\"controlExceptionText\":null,\"color\":\"#abdb2c\",\"dataRetrieving_Frequency\":0.0,\"delay_Factor\":0.0,\"disabled\":false},
{\"id\":15,\"name\":\"test\",\"controlDescription\":null,\"controlExceptionText\":null,\"color\":\"#441fed\",\"dataRetrieving_Frequency\":1440.0,\"delay_Factor\":10.0,\"disabled\":false},
{\"id\":14,\"name\":\"Powershell\",\"controlDescription\":\"text. \",\"controlExceptionText\":\"exception.\",\"color\":\"#ad89d7\",\"dataRetrieving_Frequency\":1440.0,\"delay_Factor\":10.0,\"disabled\":false}
]"

and I want to deserialize this json to fit my Control class .我想反序列化这个 json 以适合我的Control class So I did this to convert my Deserializeobject:所以我这样做是为了转换我的 Deserializeobject:

List<Control> test = JsonConvert.DeserializeObject<List<Control>>(result);

the problem is that test returns null .问题是test返回null

Any idea?!有什么想法吗?!

Update:更新:

  • This code works fine in my other app, but not in Xamarin.Form app.此代码在我的其他应用程序中运行良好,但在Xamarin.Form应用程序中Xamarin.Form
  • Tried also to append @ to the begining of the JSON string but get this error还尝试将@附加到 JSON 字符串的开头,但出现此错误

Unexpected character encountered while parsing value @.解析值 @ 时遇到意外字符。

Update:更新:

I'm using Xamarin Live Player to review my app, and Perhaps the problem is related to this, because many developers are complaining about serializing and deserializing bugs when using Live Player.我正在使用 Xamarin Live Player 来审查我的应用程序,也许问题与此有关,因为许多开发人员在使用 Live Player 时抱怨序列化和反序列化错误。

Your json string is not correct.您的 json 字符串不正确。

JSON data is written as name/value pairs. JSON 数据以名称/值对的形式写入。

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value: "name":"John"名称/值对由字段名称(双引号)、冒号和值组成: "name":"John"

Remove backslashes from your json string.从 json 字符串中删除反斜杠。 For the future you can check correctness of your json string here将来,您可以在此处检查 json 字符串的正确性

Deserialization works with this string format:反序列化适用于以下字符串格式: 在此处输入图片说明

So to answer your question: You need a valid JSON string to make the deserialization.所以要回答你的问题:你需要一个有效的 JSON 字符串来进行反序列化。 Start with a @ at the top, and replace all \\" into double quotes.从顶部的@开始,并将所有\\"替换为双引号。

Update edit: @ sign should be outside of the string, it's not part of the JSON string, it's just an indicator that your string will be multiline.更新编辑: @符号应该在字符串之外,它不是 JSON 字符串的一部分,它只是一个指示你的字符串将是多行的。

This works fine for me.这对我来说很好用。 Here's another example I have tried so far without deserialization.这是我迄今为止尝试过的另一个没有反序列化的例子。 I'm getting a response as a JSON file.我收到了 JSON 文件的响应。 my JSON looks like this.我的 JSON 看起来像这样。

在此处输入图片说明

在此处输入图片说明

..and I did this to get each elements from the JSON. ..我这样做是为了从 JSON 中获取每个元素。 and I have fit those elements to the monkeys List.我已经将这些元素放入猴子列表中。

using (JsonDocument document = JsonDocument.Parse(response, options))
            {
                Grid grid = new Grid();
                Monkeys = new List<Monkey>();
                foreach (JsonElement element in document.RootElement.EnumerateArray())
                {
                    if (element.TryGetProperty("id", out JsonElement id)&&(element.TryGetProperty("end", out JsonElement end) )
                        && (element.TryGetProperty("time", out JsonElement time) && (element.TryGetProperty("description", out JsonElement description)
                        && (element.TryGetProperty("start", out JsonElement start) && (element.TryGetProperty("date", out JsonElement date))))))
                    {    
                        Monkeys.Add(new Monkey
                        {
                            Time=time.ToString(),
                            Start = start.ToString(),
                            End =end.ToString(),
                            Description = description.ToString()
                        });
                        BindingContext = this;
                    }
                }
            }

and this is my monkey class这是我的猴子课

public class Monkey
    {
        public string Time { get; set; }
        public string Start { get; set; }
        public string End { get; set; }
        public string Description { get; set; }
        public string ImageUrl { get; set; }

        public override string ToString()
        {
            return Start;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM