title: Deserialize JSON into SQLite Database
body: <p>My model is <code>GasStation</code>.</p>
  
  <pre><code>using Newtonsoft.Json;
  using SQLite;
  using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace TDEv2.Models
  {
      public class GasStation
      {
          [JsonProperty("costcentre")][PrimaryKey]
          public string CostCentre { get; set; }
  
          [JsonProperty("name")]
          public string Name { get; set; }
  
          [JsonProperty("id")]
          public string Id { get; set; }
      }
  }
  </code></pre>
  
  <p>My <code>GasStationQuery</code> contains this:</p>
  
  <pre><code>namespace TDEv2.Models
  {
      public class GasStationQuery
      {
          public GasStation[] GasStations { get; set; }
      }
  }
  </code></pre>
  
  <p>My JSON Array looks like this:</p>
  
  <pre><code>    gasstations: [
      {
          "id": 01,
          "name": "GasStation1",
          "costcentre": 123
      },
      {
          "id": 02,
          "name": "GasStation2",
          "costcentre": 456
      }
      ]
  </code></pre>
  
  <p>Now I want to deserialize this into my SQLite database:</p>
  
  <pre><code>using SQLite;
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Threading.Tasks;
  using TDEv2.Models;
  
  namespace TDEv2.Data
  {
      public class GasStationDatabase
      {
  
          readonly SQLiteAsyncConnection database;
  
          public GasStationDatabase(string dbPath)
          {
              database = new SQLiteAsyncConnection(dbPath);
              database.CreateTableAsync&lt;GasStation&gt;().Wait();
          }
  
          public Task&lt;List&lt;GasStation&gt;&gt; GetItemsAsync()
          {
              return database.Table&lt;GasStation&gt;().ToListAsync();
          }
  
          public Task&lt;GasStation&gt; GetItemAsync(string costCentre)
          {
              return database.Table&lt;GasStation&gt;().Where(i =&gt; i.CostCentre == costCentre).FirstOrDefaultAsync();
          }
  
          public Task&lt;int&gt; SaveItemAsync(GasStation gasStation)
          {
              if (gasStation.CostCentre != null)
              {
                  return database.UpdateAsync(gasStation);
              }
              else
              {
                  return database.InsertAsync(gasStation);
              }
          }
  
      }
  }
  </code></pre>
  
  <p>Now I want to do an initial sync to fill my database to work with offline on the devices, but I don't know further steps since I am programming for not that long.</p>
  
  <p>Here's my try to fill the database:</p>
  
  <pre><code>using System.Net;
  using TDEv2.Data;
  using TDEv2.Models;
  
  namespace TDEv2.Services
  {
      public class InitialAsyncGasStationDatabase
      {
          private GasStationDatabase db;
          public GasStationQuery InitialAsyncGasStationsToDatabase()
          {
              string json;
              using (WebClient client = new WebClient())
              {
                  json = client.DownloadString($"http://xxx/gasstations.json");
              }
              foreach (GasStation gasStation in json)
              {
                  db.SaveItemAsync(gasStation);
              }
              return;
          }
      }
  }
  </code></pre>
  
  <p>The code doesn't work. I am getting an error in the <code>foreach</code> section with <code>Cannot convert type "char" to "TDEv2.Models.GasStation"</code></p>