【问题标题】:Insert JSON file in SQL tables using C# [closed]使用 C# 在 SQL 表中插入 JSON 文件 [关闭]
【发布时间】:2017-02-23 07:19:23
【问题描述】:

有人可以举个例子吗 如何使用 C# 在 SQL 表中插入 JSON 文件。

【问题讨论】:

  • 请阅读 How to Ask 并非常明确地解释您所说的“插入 JSON”是什么意思。 JSON 只是一个字符串。您想将其保存为字符串吗?那你的问题是什么?
  • 我想使用 C# 将 JSON 文件数据存储到 SQL 表中。通过读取文件并以表格格式转换数据。

标签: c# json


【解决方案1】:

假设您有以下 JSON 文件:

{
  "person":{
    "i_date":"2017-02-23",
    "i_location":"test",
    "i_summary":"test test",
    "people":[
      {
        "first_name":"first name test1",
        "last_name":"last name test1"
      },
      {
        "first_name":"first name test2",
        "last_name":"last name test2"
      },
      {
        "first_name": "first name test3",
        "last_name":"last name test3"
      }
    ]
  }
}

现在你可以声明一些代表结构的类:

public class PersonalPerson
{
    public string first_name { get; set; }
    public string last_name { get; set; }
}

public class Person
{
    public string i_date { get; set; }
    public string i_location { get; set; }
    public string i_summary { get; set; }
    public List<PersonalPerson> people { get; set; }
}

public class RootObject
{
    public Person person { get; set; }
}

最后,使用 JsonConvert.DeserializeObject 得到一组对象实例。

var root = JsonConvert.DeserializeObject<RootObject>( json );

您现在可以遍历附加到“人”的“人”并用它做一些事情。此时,您可以使用 ADO.NET 或 Entity Framework 将值从对象传输到 SQL 参数 (ADO.NET) 或 EF 类,以将其持久保存到数据库中。

我希望能为您提供所需的信息

【讨论】:

猜你喜欢
  • 2013-01-17
  • 1970-01-01
  • 2016-07-03
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多