将JSON反序列化为SQLite数据库

问题描述 投票:1回答:2

我的模特是GasStation

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

我的GasStationQuery包含这个:

namespace TDEv2.Models
{
    public class GasStationQuery
    {
        public GasStation[] GasStations { get; set; }
    }
}

我的JSON数组看起来像这样:

    gasstations: [
    {
        "id": 01,
        "name": "GasStation1",
        "costcentre": 123
    },
    {
        "id": 02,
        "name": "GasStation2",
        "costcentre": 456
    }
    ]

现在我想将其反序列化到我的SQLite数据库中:

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<GasStation>().Wait();
        }

        public Task<List<GasStation>> GetItemsAsync()
        {
            return database.Table<GasStation>().ToListAsync();
        }

        public Task<GasStation> GetItemAsync(string costCentre)
        {
            return database.Table<GasStation>().Where(i => i.CostCentre == costCentre).FirstOrDefaultAsync();
        }

        public Task<int> SaveItemAsync(GasStation gasStation)
        {
            if (gasStation.CostCentre != null)
            {
                return database.UpdateAsync(gasStation);
            }
            else
            {
                return database.InsertAsync(gasStation);
            }
        }

    }
}

现在我想进行初始同步以填充我的数据库以便在设备上脱机工作,但我不知道进一步的步骤,因为我编程的时间不长。

这是我尝试填充数据库:

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

代码不起作用。我在使用foreachCannot convert type "char" to "TDEv2.Models.GasStation"部分收到错误

c# sqlite xamarin.forms
2个回答
3
投票

您需要将json反序列化为对象,然后才能将其保存到数据库中

        using (WebClient client = new WebClient())
        {
            json = client.DownloadString($"http://xxx/gasstations.json");
        }

        // using newtonsoft json.net - use http://json2csharp.com/ to verfiy
        // that your C# model class actually matches your json
        var data = JsonConvert.DeserializeObject<GasStationQuery>(json);

        foreach (GasStation gasStation in data.GasStations)
        {
            db.SaveItemAsync(gasStation);
        }

0
投票

可能你的来源有一个GasStations列表,所以你可以将你的json对象反序列化为GasStation列表,

private GasStationDatabase db;
    public GasStationQuery InitialAsyncGasStationsToDatabase()
    {
        string json;
        using (WebClient client = new WebClient())
        {
            json = client.DownloadString($"http://xxx/gasstations.json");
        }

        var gasStationList = JsonConvert.DeserializeObject<List<GasStation>>(json);

        foreach (GasStation gasStation in gasStationList )
        {
            db.SaveItemAsync(gasStation);
        }
        return;
    }
© www.soinside.com 2019 - 2024. All rights reserved.