将Json文件转换为C#并存储在数据库中

将Json文件转换为C#并存储在数据库中,c#,json,C#,Json,我想将JSON文件转换为C#类并存储在数据库中。 我的代码 { “qqq”:“xxxx”, “rrrr”:“xxxxx”, “abc”:{ “abc1”:“xxxxx”, “abc2”:“xxxxx”, “abc3”:“xxxxx”, “abc4”:“xxxxx”, “abc5”:“2018-05-28T06:10:00.000” }, “xyx”:{ “xyz1”:“xxxxx”, “xyz2”:“xxxxx”, “xyz3”:“xxxxx”, “xyz4”:“xxxxx”, “xyz5”:

我想将JSON文件转换为C#类并存储在数据库中。 我的代码

{
“qqq”:“xxxx”,
“rrrr”:“xxxxx”,
“abc”:{
“abc1”:“xxxxx”,
“abc2”:“xxxxx”,
“abc3”:“xxxxx”,
“abc4”:“xxxxx”,
“abc5”:“2018-05-28T06:10:00.000”
},
“xyx”:{
“xyz1”:“xxxxx”,
“xyz2”:“xxxxx”,
“xyz3”:“xxxxx”,
“xyz4”:“xxxxx”,
“xyz5”:“2018-05-28T07:30:00.000”
}
},
{
“qqq”:“xxxxx”,
“rrrr”:“xxxxx”,
“abc”:{
“abc1”:“xxxxx”,
“abc2”:“xxxxx”,
“abc3”:“xxxxx”,
“abc4”:“xxxxx”,
“abc5”:“2018-05-28T06:10:00.000”
},
“xyz”:{
“xyz1”:“xxxxx”,
“xyz2”:“xxxxx”,
“xyz3”:“xxxxx”,
“xyz4”:“xxxxx”,
“xyz5”:“2018-05-28T07:30:00.000”,
}

}
在提供的JSON中,有根对象的集合,但没有一个。您必须反序列化到根对象数组中

Rootobject[] ra ;    
        ra = JsonConvert.DeserializeObject<Rootobject[]>(json);
Rootobject[]ra;
ra=JsonConvert.DeserializeObject(json);

此外,JSON中缺少[]您提供的JSON第一件事是无效的。 你可以在这里测试 有效的JSON是

[{
    "qqq": "xxxx",
    "rrrr": "xxxxx",
    "abc": {
        "abc1": "xxxxx",
        "abc2": "xxxxx",
        "abc3": "xxxxx",
        "abc4": "xxxxx",
        "abc5": "2018-05-28T06:10:00.000"
    },
    "xyx": {
        "xyz1": "xxxxx",
        "xyz2": "xxxxx",
        "xyz3": "xxxxx",
        "xyz4": "xxxxx",
        "xyz5": "2018-05-28T07:30:00.000"
    }
 }, {
    "qqq": "xxxxx",
    "rrrr": "xxxxx",
    "abc": {
        "abc1": "xxxxx",
        "abc2": "xxxxx",
        "abc3": "xxxxx",
        "abc4": "xxxxx",
        "abc5": "2018-05-28T06:10:00.000"
    },
    "xyz": {
        "xyz1": "xxxxx",
        "xyz2": "xxxxx",
        "xyz3": "xxxxx",
        "xyz4": "xxxxx",
        "xyz5": "2018-05-28T07:30:00.000"
    }
 }]
那么模型应该是

public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string qqq { get; set; }
        public string rrrr { get; set; }
        public Abc abc { get; set; }
        public Xyx xyx { get; set; }
        public Xyz xyz { get; set; }
    }

    public class Abc
    {
        public string abc1 { get; set; }
        public string abc2 { get; set; }
        public string abc3 { get; set; }
        public string abc4 { get; set; }
        public DateTime abc5 { get; set; }
    }

    public class Xyx
    {
        public string xyz1 { get; set; }
        public string xyz2 { get; set; }
        public string xyz3 { get; set; }
        public string xyz4 { get; set; }
        public DateTime xyz5 { get; set; }
    }

    public class Xyz
    {
        public string xyz1 { get; set; }
        public string xyz2 { get; set; }
        public string xyz3 { get; set; }
        public string xyz4 { get; set; }
        public DateTime xyz5 { get; set; }
    }

现在您尝试一下,如果没有,这可能会解决问题。请提及进一步的细节。

您的JSON似乎无效,反序列化的项不应该是单个对象,而应该是列表

JSON

代码

使用系统;
Net系统;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共静态void Main()
{
//json urlhttps://api.myjson.com/bins/1dbuse
WebClient c=新的WebClient();
string json=c.DownloadString(“https://api.myjson.com/bins/1dbuse");
var ra=JsonConvert.DeserializeObject(json);
Console.WriteLine(ra.First().qqq);
}
}
公共类根对象
{
公共字符串qqq{get;set;}
公共字符串rrrr{get;set;}
公共Abc{get;set;}
公共Xyz Xyz{get;set;}
}
公共课Abc
{
公共字符串abc1{get;set;}
公共字符串abc2{get;set;}
公共字符串abc3{get;set;}
公共字符串abc4{get;set;}
公共日期时间abc5{get;set;}
}
公共类Xyz
{
公共字符串xyz1{get;set;}
公共字符串xyz2{get;set;}
公共字符串xyz3{get;set;}
公共字符串xyz4{get;set;}
公共日期时间xyz5{get;set;}
}

下面是fiddle:

另外:要查看您的对象在json中的外观,您只需创建对象并将其序列化。无需创建RootObject类,可以使用JsonConvert.DeserializeObject(json)反序列化json;正如@Pranith的回答中提到的,无论如何@raj得到了解决方案。幸亏pranith@raj . 谢谢如果您能够解决问题,请将其标记为答案
public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string qqq { get; set; }
        public string rrrr { get; set; }
        public Abc abc { get; set; }
        public Xyx xyx { get; set; }
        public Xyz xyz { get; set; }
    }

    public class Abc
    {
        public string abc1 { get; set; }
        public string abc2 { get; set; }
        public string abc3 { get; set; }
        public string abc4 { get; set; }
        public DateTime abc5 { get; set; }
    }

    public class Xyx
    {
        public string xyz1 { get; set; }
        public string xyz2 { get; set; }
        public string xyz3 { get; set; }
        public string xyz4 { get; set; }
        public DateTime xyz5 { get; set; }
    }

    public class Xyz
    {
        public string xyz1 { get; set; }
        public string xyz2 { get; set; }
        public string xyz3 { get; set; }
        public string xyz4 { get; set; }
        public DateTime xyz5 { get; set; }
    }
[
  {
    "qqq": "xxxx",
    "rrrr": "xxxxx",
    "abc": {
      "abc1": "xxxxx",
      "abc2": "xxxxx",
      "abc3": "xxxxx",
      "abc4": "xxxxx",
      "abc5": "2018-05-28T06:10:00.000"
    },
    "xyx": {
      "xyz1": "xxxxx",
      "xyz2": "xxxxx",
      "xyz3": "xxxxx",
      "xyz4": "xxxxx",
      "xyz5": "2018-05-28T07:30:00.000"
    }
  },
  {
    "qqq": "xxxxx",
    "rrrr": "xxxxx",
    "abc": {
      "abc1": "xxxxx",
      "abc2": "xxxxx",
      "abc3": "xxxxx",
      "abc4": "xxxxx",
      "abc5": "2018-05-28T06:10:00.000"
    },
    "xyz": {
      "xyz1": "xxxxx",
      "xyz2": "xxxxx",
      "xyz3": "xxxxx",
      "xyz4": "xxxxx",
      "xyz5": "2018-05-28T07:30:00.000"
    }
  }
]
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    public static void Main()
    {
        //json url https://api.myjson.com/bins/1dbuse
        WebClient c = new WebClient();
        string json = c.DownloadString("https://api.myjson.com/bins/1dbuse");

        var ra = JsonConvert.DeserializeObject<List<Rootobject>>(json);
        Console.WriteLine(ra.First().qqq);
    }
}

public class Rootobject
{
    public string qqq { get; set; }
    public string rrrr { get; set; }
    public Abc abc { get; set; }
    public Xyz xyz { get; set; }

}
public class Abc
{
    public string abc1 { get; set; }
    public string abc2 { get; set; }
    public string abc3 { get; set; }
    public string abc4 { get; set; }
    public DateTime abc5 { get; set; }
}
public class Xyz
{
    public string xyz1 { get; set; }
    public string xyz2 { get; set; }
    public string xyz3 { get; set; }
    public string xyz4 { get; set; }
    public DateTime xyz5 { get; set; }
}