【问题标题】:read json file with C# and convert it into C# object用 C# 读取 json 文件并将其转换为 C# 对象
【发布时间】:2017-05-24 09:58:24
【问题描述】:

如果我的英语有时不确定,我是法国人,很抱歉... 我在工作场所工作了 6 周(我在这个领域很新……),他们希望我将实际的数据库(json)传输到 SQL 中的真实服务器,并且全部使用 C#。我需要用 C# 读取 JSON 文件,将其转换为 C# 对象。

有一个json DB的例子:

{
  "updated": "2015-05-20T13:16:00.000Z",
  "title": "Chaîne Activité",
  "description": "Vidéos des activités diverses du Webcenter",
  "items": [
    {
      "id": "77B6573B-47E4-4D60-A6FC-99E81EF05404",
      "uploaded": "2014-07-02T12:56:00.000Z",
      "uploader": "Inconnu",
      "category": "activité//Pléniére",
      "title": "Teaser de la plénière 2014",
      "description": "Marc Emmanuel de 'Tous Ensemble' et Axa Atout Coeur vous invite à la 3éme plénière.",
      "thumbnail": "/Content/img/video/activite/teaser-pleniere-2014-.png",
      "duration": 66,
      "path": "Plénière-2014-teaser.mp4"
    }`

还有我的课:

    public class Item
    {
        public string Id { get; set; }
        public string UploadDate { get; set; }
        public string Uploader { get; set; }
        public string Category { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Thumbnail { get; set; }
        public int Duration { get; set; }
        public string Path { get; set; }
    }

请记住,我不是专业人士,感谢您的帮助! :)

【问题讨论】:

  • 那你累了什么?
  • 您想使用 NewtonSoft Json nuget 包将 JSON 反序列化为您的对象。然后使用他们使用的任何 DB 技术,无论是 EF 还是 ADO 将其导入 db。
  • 我现在拥有 Newtonsoft,我会和我的“老板”一起看看 ADO

标签: c# json serialization


【解决方案1】:

你的课程应该是这样的:

public class Item
{
    public string id { get; set; }
    public string uploaded { get; set; }
    public string uploader { get; set; }
    public string category { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string thumbnail { get; set; }
    public int duration { get; set; }
    public string path { get; set; }
}

public class RootObject
{
    public string updated { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public List<Item> items { get; set; }
}

如您所见,属性名称与 json 对象中的键匹配。如果您希望 json 键具有不同的属性名称,您可以使用 Json.NET 并使用 JsonProperty 属性将正确的键映射到正确的属性。

例如:

public class Item
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("uploaded")]
    public string Uploaded { get; set; }
    [JsonProperty("uploader")]
    public string Uploader { get; set; }
    //Etc...
}

然后您的 json 字符串可以反序列化为我们的 RootObject 类:

public RootObject DeserializeJson(string jsonString)
{
    var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
    return myObj;
}

【讨论】:

  • 感谢您的回复。我对您的代码的问题是 VS 说“var”只能在脚本中使用,而 jsonString 在此范围内不存在。如果我使用 JsonProperty 是否意味着它取代了以前的课程?
  • var 是一种隐式类型,应该在您尝试反序列化 json 字符串的方法中使用。您也可以只使用显式类型RootObjectjsonString 只是我编的一个字符串变量名,你可以使用包含你的 json 字符串的变量。我已经编辑了我的答案,以包含一个更清楚的例子来说明我的意思。
  • 完成,“var”或“jsonString”不再出错,我只是在错误的地方。我必须在我的 C# 中创建任何链接来读取/转换我的 json 文件吗?
  • 这取决于您打算如何阅读它。是来自文件还是来自网站请求?
  • 当前在一个文件中。
猜你喜欢
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多