如何反序列化特定的json字符串到ASP.NET MVC模型?

时间:2019-03-17 16:55:12

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

我有这样的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字符串结构。 如果您对此有任何好的链接,我会很高兴。

3 个答案:

答案 0 :(得分: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/)上创建模型。 请告诉我这是否有帮助

答案 1 :(得分:1)

  • 底线:您可以(手动)可以,但这可能不是您想要的。
  • 原因:您的模型与JSON结构不匹配,因此是“手动”
  • 您可以在Visual Studio或VS Code中使用随时可用的工具来帮助您创建正确的模型(例如Paste JSON As Code
  • 一旦您准备好“适当的”模型,请仔细检查JSON documentation for (de)serializing

答案 2 :(得分:1)

我必须修复您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时可以使用的代码段。此代码段使用Json.NET库。

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