猿问

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

我想将 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])反序列化为类型“testProject.Form1+Rootobject”,因为该类型需要一个 JSON 对象(例如 {\"name\":\"value\ "}) 以正确反序列化。\r\n要修复此错误,请将 JSON 更改为 JSON 对象


长风秋雁
浏览 327回答 3
3回答

缥缈止盈

在提供的 JSON 中,有 RootObjects 的集合但不是一个。您必须反序列化为 RootObjects 数组。Rootobject[]&nbsp;ra&nbsp;;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ra&nbsp;=&nbsp;JsonConvert.DeserializeObject<Rootobject[]>(json);JSON 中也缺少 []

德玛西亚99

第一件事 您提供的 JSON 无效。你可以在这里测试JsonLint 有效的 JSON 是..[{&nbsp; &nbsp; "qqq": "xxxx",&nbsp; &nbsp; "rrrr": "xxxxx",&nbsp; &nbsp; "abc": {&nbsp; &nbsp; &nbsp; &nbsp; "abc1": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc2": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc3": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc4": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc5": "2018-05-28T06:10:00.000"&nbsp; &nbsp; },&nbsp; &nbsp; "xyx": {&nbsp; &nbsp; &nbsp; &nbsp; "xyz1": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz2": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz3": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz4": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz5": "2018-05-28T07:30:00.000"&nbsp; &nbsp; }&nbsp;}, {&nbsp; &nbsp; "qqq": "xxxxx",&nbsp; &nbsp; "rrrr": "xxxxx",&nbsp; &nbsp; "abc": {&nbsp; &nbsp; &nbsp; &nbsp; "abc1": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc2": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc3": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc4": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "abc5": "2018-05-28T06:10:00.000"&nbsp; &nbsp; },&nbsp; &nbsp; "xyz": {&nbsp; &nbsp; &nbsp; &nbsp; "xyz1": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz2": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz3": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz4": "xxxxx",&nbsp; &nbsp; &nbsp; &nbsp; "xyz5": "2018-05-28T07:30:00.000"&nbsp; &nbsp; }&nbsp;}]那么模型应该是public class Rootobject&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Class1[] Property1 { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Class1&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string qqq { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string rrrr { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public Abc abc { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public Xyx xyx { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public Xyz xyz { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Abc&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string abc1 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string abc2 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string abc3 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string abc4 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime abc5 { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Xyx&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string xyz1 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz2 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz3 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz4 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime xyz5 { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Xyz&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string xyz1 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz2 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz3 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string xyz4 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime xyz5 { get; set; }&nbsp; &nbsp; }现在你试试,也许这会解决问题,如果没有请提及更多细节。
随时随地看视频慕课网APP
我要回答