【问题标题】:Deserialized Json object value is null反序列化的 Json 对象值为 null
【发布时间】:2021-12-15 18:18:21
【问题描述】:

我无法从 OneDay.price_change 获得价值。 HTTP 响应正常,我得到以下信息:

HTTP 响应

 "[{\"id\":\"BTC\",\"currency\":\"BTC\",\"symbol\":\"BTC\",\"name\":\"Bitcoin\",\"logo_url\":\"https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/btc.svg\",\"status\":\"active\",\"price\":\"60947.08258854\",\"price_date\":\"2021-10-31T00:00:00Z\",\"price_timestamp\":\"2021-10-31T18:51:00Z\",\"circulating_supply\":\"18860037\",\"max_supply\":\"21000000\",\"market_cap\":\"1149464232662\",\"market_cap_dominance\":\"0.4078\",\"num_exchanges\":\"397\",\"num_pairs\":\"67587\",\"num_pairs_unmapped\":\"5196\",\"first_candle\":\"2011-08-18T00:00:00Z\",\"first_trade\":\"2011-08-18T00:00:00Z\",\"first_order_book\":\"2017-01-06T00:00:00Z\",\"rank\":\"1\",\"high\":\"66082.82561618\",\"high_timestamp\":\"2021-10-20T00:00:00Z\",\"1h\":{\"volume\":\"1248590564.91\",\"price_change\":\"-85.32656234\",\"price_change_pct\":\"-0.0014\",\"volume_change\":\"-218879322.04\",\"volume_change_pct\":\"-0.1492\",\"market_cap_change\":\"-1607003923.65\",\"market_cap_change_pct\":\"-0.0014\"},\"1d\":{\"volume\":\"39937857069.60\",\"price_change\":\"-845.68642611\",\"price_change_pct\":\"-0.0137\",\"volume_change\":\"1918883279.43\",\"volume_change_pct\":\"0.0505\",\"market_cap_change\":\"-15892518975.54\",\"market_cap_change_pct\":\"-0.0136\"}}]\n"

但是,由于某种原因,我无法接受 1d 的价格变化。我不确定可能是什么问题。任何帮助表示赞赏!

型号:

   public class OneHour
    {
        public string Volume { get; set; }
        public string Price_change { get; set; }
        public string Price_change_pct { get; set; }
        public string Volume_change { get; set; }
        public string Volume_change_pct { get; set; }
        public string Market_cap_change { get; set; }
        public string Market_cap_change_pct { get; set; }
    }
    public class OneDay
    {
        public string Volume { get; set; }
        public string Price_change { get; set; }
        public string Price_change_pct { get; set; }
        public string Volume_change { get; set; }
        public string Volume_change_pct { get; set; }
        public string Market_cap_change { get; set; }
        public string Market_cap_change_pct { get; set; }
    }


    public class CryptoApiMain
    {
        public OneHour OneHour { get; set; }

        public OneDay OneDay { get; set; }

        public string Id { get; set; }

        public string Symbol { get; set; }

        public string Status { get; set; }

        public double Price { get; set; }

        public string Price_date { get; set; }

        public string Circulating_supply { get; set; }

        public string Num_exchanges { get; set; }


        public string Num_pairs { get; set; }

        public string Rank { get; set; }

        public string High { get; set; }

    }
    var theresponse = settingsService.CryptoApiResult(cryptoStock).Result;
        foreach (var rez in theresponse)
        {
           <span id="stockSymbolCrypto">@cryptoStock</span> 
     
<p>$@Convert.ToInt64(@rez.Price) @rez.OneDay.Price_change</p>
    
      }

@rez.OneDay.Price_change 错误弹出窗口

【问题讨论】:

  • 您尝试访问它的代码在哪里,您会遇到什么错误?
  • 您需要设置 JSON 属性以获取正确的值 - 1h。你如何做到这一点取决于你使用什么序列化器。另请注意,JSON 反序列化通常区分大小写。
  • 另外,您不需要为 OneHour 和 OneDay 复制相同的课程。
  • 值反序列化后会进入哪个字段?当名称不匹配时,您需要告诉反序列化器。
  • 我遵循了一个例子,但似乎没有必要。我解决了问题,谢谢提示

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


【解决方案1】:

您的“ public OneHour OneHour { get; set; } ”和“ public OneDay OneDay { get; set; } ”属性应该绑定到 [JsonProperty("1h")] 和 [JsonProperty("1d")]

试试这个

CryptoApiMain[] jsond = JsonConvert.DeserializeObject<CryptoApiMain[]>(json);

var price = jsond[0].OneDay.PriceChange;

结果

-845.68642611

类

 public partial class CryptoApiMain
    {
        [JsonProperty("1h")]
        public One OneHour { get; set; }
        [JsonProperty("1d")]
        public One OneDay { get; set; }
        
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("currency")]
        public string Currency { get; set; }

        [JsonProperty("symbol")]
        public string Symbol { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("logo_url")]
        public Uri LogoUrl { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("price")]
        public string Price { get; set; }

        [JsonProperty("price_date")]
        public DateTimeOffset PriceDate { get; set; }

        [JsonProperty("price_timestamp")]
        public DateTimeOffset PriceTimestamp { get; set; }

        [JsonProperty("circulating_supply")]
        public long CirculatingSupply { get; set; }

        [JsonProperty("max_supply")]
        public long MaxSupply { get; set; }

        [JsonProperty("market_cap")]
        public string MarketCap { get; set; }

        [JsonProperty("market_cap_dominance")]
        public string MarketCapDominance { get; set; }

        [JsonProperty("num_exchanges")]
    
        public long NumExchanges { get; set; }

        [JsonProperty("num_pairs")]
        
        public long NumPairs { get; set; }

        [JsonProperty("num_pairs_unmapped")]
        public long NumPairsUnmapped { get; set; }

        [JsonProperty("first_candle")]
        public DateTimeOffset FirstCandle { get; set; }

        [JsonProperty("first_trade")]
        public DateTimeOffset FirstTrade { get; set; }

        [JsonProperty("first_order_book")]
        public DateTimeOffset FirstOrderBook { get; set; }

        [JsonProperty("rank")]
        public long Rank { get; set; }

        [JsonProperty("high")]
        public string High { get; set; }

        [JsonProperty("high_timestamp")]
        public DateTimeOffset HighTimestamp { get; set; }
}

public partial class One
{
    [JsonProperty("volume")]
    public string Volume { get; set; }

       [JsonProperty("price_change")]
        public string PriceChange { get; set; }

        [JsonProperty("price_change_pct")]
        public string PriceChangePct { get; set; }

        [JsonProperty("volume_change")]
        public string VolumeChange { get; set; }

        [JsonProperty("volume_change_pct")]
        public string VolumeChangePct { get; set; }

        [JsonProperty("market_cap_change")]
        public string MarketCapChange { get; set; }

        [JsonProperty("market_cap_change_pct")]
        public string MarketCapChangePct { get; set; }
    }

【讨论】:

    【解决方案2】:

    问题是您在 json 中的属性名称 (1d) 和 c# 模型中的属性名称 (OneDay) 不匹配。

    如果您使用的是System.Text.Json(.Net Core 3.0 和更高版本),请使用以下内容

    [JsonPropertyName("1d")]
    public OneDay OneDay { get; set; }
    

    如果您使用的是Newtonsoft(.Net Core 3.0 之前),请使用以下内容

    [JsonProperty(PropertyName = "1d")]
    public OneDay OneDay { get; set; }
    

    【讨论】:

    • 我知道这一点,但问题是我不能用数字名称声明一个类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多