Home > database >  Xamarin SQLite deserialize list
Xamarin SQLite deserialize list

Time:05-12

I need to store a list inside my Xamarin Forms App. For the ticket model i converted the JSON to C# via json2csharp. Also added the TextBlob otherwise it would tell me: SQLite cant read the data type (list). With the current code i dont get an error but notes wont be added into the database.

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; }
    }

SaveTicket.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");
            }
        }

CodePudding user response:

I don't known whether you use the nuget package SQLite-net pcl or not. It seems that the package doesn't support to insert the list into the datebase.

At first, you can add a break point to the Api api = JsonConvert.DeserializeObject<Api>(responseBody); to check the value of the list is null or not.

And then you can use the SQLiteNetExtensions package which can insert the list into the table.

You can check the two link:How can you store lists of objects in SQLite.net? and https://social.msdn.microsoft.com/Forums/en-US/ca6c1b6f-bf83-488e-a79a-9b9ac48575a0/use-listltgt-in-database-using-sqlitenet-pcl-library?forum=xamarinforms

  • Related