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

时间:2018-05-29 05:57:07

标签: c# json

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

string json = new System.Net.WebClient().DownloadString(Path);



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




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

我收到以下错误 无法将当前JSON数组(例如[1,2,3])反序列化为类型&#39; testProject.Form1 + Rootobject&#39;因为类型需要一个JSON对象(例如{\&#34; name \&#34;:\&#34; value \&#34;})才能正确反序列化。\ r \ n要修复此错误要么更改JSON到JSON对象

3 个答案:

答案 0 :(得分:0)

在提供的JSON中,有RootObject集合但不是一个。您必须反序列化为RootObjects数组。

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

JSON中还缺少[]

答案 1 :(得分:0)

首先,您提供的JSON无效。 你可以在这里测试JsonLint 有效的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"
    }
 }]

那么Model应该是

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

现在你试试,如果不是这样就可以解决问题,请提及更多细节。

答案 2 :(得分:0)

您的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"
    }
  }
]

CODE

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

这里的小提琴:https://dotnetfiddle.net/AmWnxF