Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析JSON文件并将数据插入SQL server_C#_Sql_Json.net - Fatal编程技术网

C# 解析JSON文件并将数据插入SQL server

C# 解析JSON文件并将数据插入SQL server,c#,sql,json.net,C#,Sql,Json.net,我有以下JSON对象,正在尝试创建插入查询。创建数据并将其插入数据库的最佳方法是什么?我正在使用JSON.NET解析该文件。我感谢你的建议 JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { if(reader.Value != null) Console.WriteLine("Field: {0}, Value: {1}", reader.Toke

我有以下JSON对象,正在尝试创建插入查询。创建数据并将其插入数据库的最佳方法是什么?我正在使用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" 
      }
    }
  }
}

首先,我将重新构造JSON,使其更有意义。你说过一个“人”可以有多个“人”,所以用这种方式构造它。“人员”有三个属性(即i_日期、i_位置和i_摘要)和人员集合

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

此时,您可以使用ADO.NET或Entity Framework将对象中的值转换为SQL参数(ADO.NET)或EF类,以将其持久化到数据库中。

我们讨论的是哪种RDBMS?您想使用LINQ吗?我正在使用SQL server。我可以用LINQ。数据应该放在两个单独的表格中。我正在尝试将个人数据(I_日期、I_位置、I_摘要)插入表A和表B中的people1、people2和people3中。这是您必须处理的一些丑陋的JSON。您是否可以控制传入JSON的格式?如果你这样做,我建议将“人”改为一组对象。在“people”中有一系列对象,每个对象都有不同的名称,这比需要的困难得多,除非“people”下总是正好有三个对象。是的。我可以控制格式。“人”下的对象有时会超过3个(最多11个)或更少。你能建议一下格式吗?我对你的数据中的关系还是有点困惑。你有人(意味着多人),然后你有一个人(可能有多人吗?),在这个人下面你有人(意味着每个人有多人)。这只是一种非常混乱的关系。如果你能更新你的问题,用商业术语解释这些关系,可能会有所帮助。我正在获取myJsonObject为null的信息。您能告诉我可能出了什么问题吗?也许您输入到调用中的JSON是空的。您是否尝试过调试代码以确保传递的字符串具有正确的格式?我看到有一个JSON字符串,它是上面的格式。
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 );
}