c# - 当 json 中的 id 属性充当对象时创建 POCO

标签 c# asp.net json asp.net-mvc json.net

这是我第一次遇到这种困难。我正在尝试使用 newtonsoft.net 用 c# 解析一个简单的 json,但我不知道如何处理此类问题。我的 json 是这种格式:

    {
      "events": {
        "119327": {
          "name": "John doe",
          "start": "1524435387600",
          "end": "152446565612800",
          "details": "description"
          "image_url": "http://go/to/url"
        },
        "119217": {
          "name": "John doe",
          "start": "1524534243387600",
          "end": "15244656612800",
          "details": "description"
          "image_url": "http://go/to/url"
        },
        "14397": {
          "name": "John doe",
          "start": "1524386567600",
          "end": "15244143242800",
          "details": "description"
          "image_url": "http://go/to/url"
        }
      }
    }  

现在您可以看到 id 的行为就像一个对象,也像 id 本身一样,稍后我将需要它。 谁能向我解释一下如何解决这个问题。有什么办法吗?任何帮助将不胜感激。提前致谢。

最佳答案

我会定义一些像这样的“事件”类

      name
      start
      end
      detail
      image_url

然后,我会尝试反序列化events作为Dictionary<int, Event> .

之后,您始终可以在生成的字典上使用一些 LINQ 查询,将其映射到另一个类似于具有附加 id 字段的 Event 类的 EventWithId 类。

关于c# - 当 json 中的 id 属性充当对象时创建 POCO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883999/

相关文章:

c# - 如何在互联网中断后继续或恢复 FTP 上传

c# - 使用 Inno Setup 安装具有不同配置的同一 Windows 服务的多个实例

c# - 如何在 ASP.NET MVC 中处理同一窗体上的编辑和删除按钮?

json - 改造管理无对象模型列表JSON

php - 通过某些路由前缀捕获未找到错误 [Laravel]

Javascript - 从 JSON 到对象的数组

c# - 如何将 bool 保存到 PlayerPrefs Unity

c# - 将 C# CryptoSys 辅助的 3DES 加密移植到 Objective-C CommonCrypto 问题

asp.net - 为什么 ASP.NET Web 服务占用如此多的内存?

jquery - 如何使用 ajax 发送 XML?