【问题标题】:How to deserialize specific json string to ASP.NET MVC model?如何将特定的 json 字符串反序列化为 ASP.NET MVC 模型?
【发布时间】:2019-03-17 16:55:11
【问题描述】:

我有这样的json字符串:

{
    "data": [
        {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            ...
            "quote": {
                "USD": {
                    "price": 9283.92,
                    "volume_24h": 7155680000,
                    "percent_change_1h": -0.152774,
                    "percent_change_24h": 0.518894,
                    "market_cap": 158055024432,
                    "last_updated": "2018-08-09T22:53:32.000Z"
                },
                "BTC": {
                    "price": 1,
                    "volume_24h": 772012,
                    "percent_change_1h": 0,
                    "percent_change_24h": 0,
                    "percent_change_7d": 0,
                    "market_cap": 17024600,
                    "last_updated": "2018-08-09T22:53:32.000Z"
                }
            }
        },
        // objects like previous from which i need the data
    ],
    "status": {
        "timestamp": "2018-06-02T22:51:28.209Z",
        ...
    }
}

我如何将它反序列化成这样的模型:

public class MyModel
{
    public string Name { get; set; }
    public string Symbol { get; set; }
    public string Price { get; set; }
    public double Percent_change_1h { get; set; }
    public double Percent_change_24h { get; set; }
    public long Market_cap { get; set; }
    public DateTime Last_updated { get; set; }
}

模型中的字段名与json字符串中的键名相同。

我是 C# 新手,我找不到任何关于我的问题的有用信息,尤其是因为这个特定的 json 字符串结构。 如果你给我任何关于这个的好链接,我会很高兴。

【问题讨论】:

  • 当您有 json 字符串并希望将其转换为模型类时。然后复制该 json 并转到 C# 中的一个类,然后单击顶部的编辑菜单 > 选择性粘贴 > 将 Json 粘贴为类。它将为您的 json 字符串生成模型。

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


【解决方案1】:

模型好像是这样的。

public class Model
{
   public List<Datum> data { get; set; }
   public Status status { get; set; }
}

public class Status
{
    public DateTime timestamp { get; set; }
}

public class Datum
{
    public int id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public Quote quote { get; set; }
}
public class Quote
{
    public USD USD { get; set; }
   public BTC BTC { get; set; }
}
public class BTC
{
    public int price { get; set; }
    public int volume_24h { get; set; }
    public int percent_change_1h { get; set; }
    public int percent_change_24h { get; set; }
    public int percent_change_7d { get; set; }
    public int market_cap { get; set; }
    public DateTime last_updated { get; set; }
}

public class USD
{
    public double price { get; set; }
    public object volume_24h { get; set; }
    public double percent_change_1h { get; set; }
    public double percent_change_24h { get; set; }
    public object market_cap { get; set; }
    public DateTime last_updated { get; set; }
}

您也可以尝试通过复制有效的 json 字符串在 (http://json2csharp.com/) 上创建模型。 如果这有帮助,请告诉我

【讨论】:

    【解决方案2】:
    • 底线:您可以(手动),但这可能不是您想要的。
    • 原因:您的模型与 JSON 结构不匹配,因此是“手动”
    • 您可以使用 Visual Studio 或 VS Code 中现成的工具来帮助您创建正确的模型(例如 Paste JSON As Code
    • 准备好“合适的”模型后,请查看JSON documentation for (de)serializing

    【讨论】:

      【解决方案3】:

      我必须修复你的 json 上的一些语法错误,所以修复的版本如下:

      {
          "data": [
            {
              "id": 1,
              "name": "Bitcoin",
              "symbol": "BTC",
              "quote": {
                "USD": {
                  "price": 9283.92,
                  "volume_24h": 7155680000,
                  "percent_change_1h": -0.152774,
                  "percent_change_24h": 0.518894,
                  "market_cap": 158055024432,
                  "last_updated": "2018-08-09T22:53:32.000Z"
                },
                "BTC": {
                  "price": 1,
                  "volume_24h": 772012,
                  "percent_change_1h": 0,
                  "percent_change_24h": 0,
                  "percent_change_7d": 0,
                  "market_cap": 17024600,
                  "last_updated": "2018-08-09T22:53:32.000Z"
                }
              }
            }
          ],
          "status": {
              "timestamp": "2018-06-02T22:51:28.209Z"
          }
      }
      

      这是与以前的 json 匹配的 C# 模型类:

      public class Rootobject
          {
              public Datum[] data { get; set; }
              public Status status { get; set; }
          }
      
          public class Status
          {
              public DateTime timestamp { get; set; }
          }
      
          public class Datum
          {
              public int id { get; set; }
              public string name { get; set; }
              public string symbol { get; set; }
              public Quote quote { get; set; }
          }
      
          public class Quote
          {
              public USD USD { get; set; }
              public BTC BTC { get; set; }
          }
      
          public class USD
          {
              public float price { get; set; }
              public long volume_24h { get; set; }
              public float percent_change_1h { get; set; }
              public float percent_change_24h { get; set; }
              public long market_cap { get; set; }
              public DateTime last_updated { get; set; }
          }
      
          public class BTC
          {
              public int price { get; set; }
              public int volume_24h { get; set; }
              public int percent_change_1h { get; set; }
              public int percent_change_24h { get; set; }
              public int percent_change_7d { get; set; }
              public int market_cap { get; set; }
              public DateTime last_updated { get; set; }
          }
      

      这是反序列化 json 时可以使用的代码 sn-p。这个 sn-p 使用Json.NET-library。

      var obj = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("object.json"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多