Xamarin SQLite 反序列化列表

Xamarin SQLite deserialize list

我需要在我的 Xamarin Forms 应用程序中存储一个列表。对于票据模型,我通过 json2csharp 将 JSON 转换为 C#。还添加了 TextBlob,否则它会告诉我:SQLite 无法读取数据类型(列表)。使用当前代码我没有收到错误,但不会将注释添加到数据库中。

JSON

{
    "ticket": {
        "id": 2344425,
        "ticketNumber": "19783",
        "subject": "Titel",
        "description": "<h1>Beschreibung</h1><p>Testbeschreibung</p><h2>Device</h2><ul><li><b>manufacturer:</b>Apple18</li><li><b>model:</b>AOSP on IA Emulator</li><li><b>phonenumber:</b>+49 1234 567899</li></ul>",
        "notes": [],
        "status": "open"
    }
}

Ticket.cs

    public class Ticket
    {
        [PrimaryKey, AutoIncrement]
        public int pkid { get; set; }
        public string id { get; set; }
        public string ticketNumber { get; set; }
        public string status { get; set; }
        public string subject { get; set; }
        public string description { get; set; }
        [TextBlob("notesBlobbed")]
        public List<string> notes { get; set; }
        public string notesBlobbed { get; set; }
        public DateTime closed { get; set; }
        public DateTime updated { get; set; }
    }

保存Ticket.cs

public async void GeneralButton(object sender, EventArgs e)
        {
            //Wertzuweisung
            General g = new General();
            g.manufacturer = ManufacturerInfo.Text;
            g.model = ModelInfo.Text;
            g.subject = SubjectInfo.Text;
            g.description = DescriptionInfo.Text;

            //Verbindungsaufbau
            var url = "https://.../api/ticket/create";
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("AuthenticationToken", "Token");
            string json = JsonConvert.SerializeObject(new { g.manufacturer, g.model, g.subject, g.description });
            HttpContent content = new StringContent(json);
            HttpResponseMessage response = await client.PostAsync(url, content);

            //prüft ob alle Felder ausgefüllt sind + Internetverbindung besteht
            if (response.IsSuccessStatusCode)
            {
                //response initalisieren
                string responseBody = await response.Content.ReadAsStringAsync();
                Api api = JsonConvert.DeserializeObject<Api>(responseBody);

                //Ticket speichern in Datenbank
                await App.Database.SaveTicket(new Ticket
                {
                    pkid = 1,
                    id = api.ticket.id,
                    ticketNumber = api.ticket.ticketNumber,
                    status = api.ticket.status,
                    subject = api.ticket.subject,
                    description = api.ticket.description,
                    notes = api.ticket.notes
                });
                await DisplayAlert("Alert", "Das Ticket wurde erfolgreich erstellt.", "Ok");
            }
            else
            {
                await DisplayAlert("Alert", "Die Verbindung ist fehlgeschlagen füllen Sie alle Felder aus und prüfen Sie Ihre Internetverbindung.", "Ok");
            }
        }

不知道你有没有用nuget包SQLite-netpcl。似乎该包不支持将列表插入数据库。

首先,您可以在Api api = JsonConvert.DeserializeObject<Api>(responseBody);中添加一个断点来检查列表的值是否为空。

然后您可以使用 SQLiteNetExtensions 包将列表插入 table。

您可以检查两个 link:https://social.msdn.microsoft.com/Forums/en-US/ca6c1b6f-bf83-488e-a79a-9b9ac48575a0/use-listltgt-in-database-using-sqlitenet-pcl-library?forum=xamarinforms