【问题标题】:parse JSON file and insert data into SQL server解析 JSON 文件并将数据插入 SQL Server
【发布时间】:2014-04-27 22:29:48
【问题描述】:

我有以下 JSON 对象并尝试创建 INSERT 查询。创建数据并将数据插入数据库的最佳方法是什么?我正在使用 JSON.NET 来解析文件。我很感激任何建议。

JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
    if(reader.Value != null)
    Console.WriteLine("Field: {0}, Value: {1}", reader.TokenType, reader.Value);
}

这是我的 JSON 的样子。

{
  "persons": {
    "person": {
      "i_date":  "2014-03-20", 
      "i_location": "test", 
      "i_summary": "test test" 
    },
    "people": {
      "people1": {
        "first_name": "first name test1", 
        "last_name": "last name test1"  
      },
      "people2": {
        "first_name": "first name test2", 
        "last_name": "last name test2" 
      }, 
      "people3": {
        "first_name": "first name test3", 
        "last_name": "last name test3" 
      }
    }
  }
}

【问题讨论】:

  • 我们在谈论哪个 RDBMS?你想使用 LINQ 吗?
  • 我正在使用 SQL 服务器。我可以使用 LINQ。数据应转到两个单独的表。我正在尝试将人员数据(i_date、i_location、i_summary)插入 TABLE_A 和 TABLE_B 中的 people1、people2 和 people3。
  • 您必须处理一些丑陋的 JSON。您是否可以控制传入 JSON 的格式?如果你这样做,我建议将“人”更改为对象数组。在“人”中包含一系列对象,每个对象都具有不同的名称,这比实际需要的要困难得多,除非“人”下总是正好有三个对象。
  • 是的。我可以控制格式。 “人”下有时会有超过 3 个(最多 11 个)对象或更少。你能推荐一下格式吗?
  • 我对您数据中的关系仍然有些困惑。你有人(暗示多人)然后你有一个人(可以有不止一个人吗?)在这个人之下你有人(暗示每人不止一个人)。这只是一个非常混乱的关系。如果您可以更新您的问题以用业务术语解释这些关系,那可能会有所帮助。

标签: c# sql json.net


【解决方案1】:

首先,我将重组 JSON,使其更有意义。你说一个“人”可以有多个“人”,所以这样组织。 “人”具有三个属性(即 i_date、i_location 和 i_summary)和人员集合。

{
  "person":{
    "i_date":"2014-03-20",
    "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"
      }
    ]
  }
}

现在您可以声明一些表示结构的 .NET 类。

public class Person2
{
    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<Person2> people { get; set; }
}

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

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

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

您现在可以遍历附加到“人”的“人”并用它做一些事情。

Console.WriteLine( root.person.i_date );
Console.WriteLine( root.person.i_location );
Console.WriteLine( root.person.i_summary );

foreach(var p in root.person.people)
{
    Console.WriteLine( p.first_name );
    Console.WriteLine( p.last_name );
}

此时,您可以使用 ADO.NET 或实体框架将值从对象传输到 SQL 参数 (ADO.NET) 或 EF 类,以将其持久保存到数据库中。

【讨论】:

  • 我遵循了相同的代码。我得到 myJsonObject 为空。你能告诉我可能出了什么问题吗?
  • 您输入调用的 JSON 可能是空的。您是否尝试过调试代码以确保您传入的字符串格式正确?
  • 我看到有一个 JSON 字符串,它是上述格式。
猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多