【问题标题】:How do i hard code JSON for my given class to store in database我如何为给定的类硬编码 JSON 以存储在数据库中
【发布时间】:2016-07-31 12:23:00
【问题描述】:
using System;
using SQLite;

namespace Students
{
public class Student
{
    public Student()
    {
    }

    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public string Name { get; set; }

    public string percent { get; set; }

    public string rollNo { get; set; }

}
}

这是我用来实例化学生类型对象的 SQLite 数据库类。我现在想要的很简单,一个简单的硬编码 JSON 对象,用 C# 声明,序列化,我可以在反序列化后存储到这个数据库中(只是为了测试它是否工作)

我试图存储在数据库中的示例 JSON 是:

[
  {
    "name": "Gopi",
    "rollNo": "13",
    "%age": "33"
  },
  {
    "name": "Topi",
    "rollNo": "23",
    "%age": "43"
  },
  {
   "name": "Kopi",
   "rollNo": "33",
   "%age": "53"
   }
]

我想在用 C# 解析这个 JSON 对象后将这些值存储到我的 SQLite 数据库中。我不确定如何继续。

【问题讨论】:

    标签: c# json sqlite rest


    【解决方案1】:

    创建一些处理类,例如:

    public class Handler{
    public string name { get; set; }
    public string rollNo { get; set; }
    public string age { get; set; }}
    

    然后:

    public static HttpClient CreateClient()
        {
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri(someURL)
            };
    
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
            return httpClient;
        }
    
    public static async Task<string> Get(string path)
    {
        using (var client = CreateClient())
        {
            var getResponse = await client.GetAsync(path);
            return await getResponse.Content.ReadAsStringAsync();
    
        }
    } 
    
    
    
    public async Task<string> UpdateFriendsLocation()
        {
            var response = await WebServices.Get(someURL);
            return response;
        }
    
    //In the place you want
    var resp = JsonConvert.DeserializeObject<Handler>(response);
    

    在你的处理程序中使用这个你有数据,然后你必须在你的 SQLite 中从该数据中进行输入。

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2020-04-14
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多