代码之家  ›  专栏  ›  技术社区  ›  Phillip McMullen

如何正确反序列化此对象?

  •  -1
  • Phillip McMullen  · 技术社区  · 8 年前

    这可能很简单,请提前道歉。我只是对它感到沮丧,我对JSON的容忍度会随着时间的推移而动摇。

    字符串可以对键进行反序列化,但当Quote对象尝试反序列化时,运气不佳。构造Quote对象,但所有值都为null/默认为其对象的null值(0表示int/double,null表示string等)。“quote”属性未反序列化到对象。

    我试过了

    JsonConvert.DeserializeObject<Dictionary<string, Quote>>
    

    “报价”如下:

        [JsonObject("quote")]
        public class Quote
        {
            public string symbol { get; set; }
            public string companyName { get; set; }
            public string primaryExchange { get; set; }
            public string sector { get; set; }
            public string calculationPrice { get; set; }
            public float open { get; set; }
            public long openTime { get; set; }
            public float close { get; set; }
            public long closeTime { get; set; }
            public float high { get; set; }
            public float low { get; set; }
            public float latestPrice { get; set; }
            public string latestSource { get; set; }
            public string latestTime { get; set; }
            public long latestUpdate { get; set; }
            public int latestVolume { get; set; }
            public float iexRealtimePrice { get; set; }
            public int iexRealtimeSize { get; set; }
            public long iexLastUpdated { get; set; }
            public float delayedPrice { get; set; }
            public long delayedPriceTime { get; set; }
            public float previousClose { get; set; }
            public float change { get; set; }
            public float changePercent { get; set; }
            public float iexMarketPercent { get; set; }
            public int iexVolume { get; set; }
            public int avgTotalVolume { get; set; }
            public float iexBidPrice { get; set; }
            public int iexBidSize { get; set; }
            public float iexAskPrice { get; set; }
            public int iexAskSize { get; set; }
            public long marketCap { get; set; }
            public float peRatio { get; set; }
            public float week52High { get; set; }
            public float week52Low { get; set; }
            public float ytdChange { get; set; }
        }
    

    JSON如下所示:

    {
      "AMAT": {
        "quote": {
          "symbol": "AMAT",
          "companyName": "Applied Materials Inc.",
          "primaryExchange": "Nasdaq Global Select",
          "sector": "Technology",
          "calculationPrice": "tops",
          "open": 55.87,
          "openTime": 1520001000554,
          "close": 57.07,
          "closeTime": 1519938000425,
          "high": 57.619,
          "low": 55.12,
          "latestPrice": 57.65,
          "latestSource": "IEX real time price",
          "latestTime": "12:56:33 PM",
          "latestUpdate": 1520013393045,
          "latestVolume": 8005359,
          "iexRealtimePrice": 57.65,
          "iexRealtimeSize": 100,
          "iexLastUpdated": 1520013393045,
          "delayedPrice": 57.42,
          "delayedPriceTime": 1520012500382,
          "previousClose": 57.07,
          "change": 0.58,
          "changePercent": 0.01016,
          "iexMarketPercent": 0.03564,
          "iexVolume": 285311,
          "avgTotalVolume": 16065459,
          "iexBidPrice": 56.06,
          "iexBidSize": 100,
          "iexAskPrice": 58.63,
          "iexAskSize": 100,
          "marketCap": 60572134433,
          "peRatio": 17.74,
          "week52High": 60.89,
          "week52Low": 36.33,
          "ytdChange": 0.075980392156863
        }
      },
      "AAPL": {
        "quote": {
          "symbol": "AAPL",
          "companyName": "Apple Inc.",
          "primaryExchange": "Nasdaq Global Select",
          "sector": "Technology",
          "calculationPrice": "tops",
          "open": 172.67,
          "openTime": 1520001000489,
          "close": 175,
          "closeTime": 1519938000498,
          "high": 175.67,
          "low": 172.45,
          "latestPrice": 175.92,
          "latestSource": "IEX real time price",
          "latestTime": "12:56:35 PM",
          "latestUpdate": 1520013395847,
          "latestVolume": 21945163,
          "iexRealtimePrice": 175.92,
          "iexRealtimeSize": 100,
          "iexLastUpdated": 1520013395847,
          "delayedPrice": 175.299,
          "delayedPriceTime": 1520012500554,
          "previousClose": 175,
          "change": 0.92,
          "changePercent": 0.00526,
          "iexMarketPercent": 0.03742,
          "iexVolume": 821188,
          "avgTotalVolume": 45362032,
          "iexBidPrice": 173,
          "iexBidSize": 200,
          "iexAskPrice": 175.89,
          "iexAskSize": 100,
          "marketCap": 892620366960,
          "peRatio": 19.12,
          "week52High": 180.615,
          "week52Low": 137.05,
          "ytdChange": 0.015906188319981
        }
      }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   FaizanHussainRabbani    8 年前

    因为你必须选择不同的对象,即 AMAT & AAPL :

    public class ToBeDeserialized
    {
        public AMAT AMAT { get; set; }
        public AAPL AAPL { get; set; }
    }
    
    public class AMAT
    {
        public Quote quote { get; set; }
    }
    
    public class AAPL
    {
        public Quote quote { get; set; }
    }
    
    public class Quote
    {
        public string symbol { get; set; }
        public string companyName { get; set; }
        public string primaryExchange { get; set; }
        public string sector { get; set; }
        public string calculationPrice { get; set; }
        public float open { get; set; }
        public long openTime { get; set; }
        public float close { get; set; }
        public long closeTime { get; set; }
        public float high { get; set; }
        public float low { get; set; }
        public float latestPrice { get; set; }
        public string latestSource { get; set; }
        public string latestTime { get; set; }
        public long latestUpdate { get; set; }
        public int latestVolume { get; set; }
        public float iexRealtimePrice { get; set; }
        public int iexRealtimeSize { get; set; }
        public long iexLastUpdated { get; set; }
        public float delayedPrice { get; set; }
        public long delayedPriceTime { get; set; }
        public float previousClose { get; set; }
        public float change { get; set; }
        public float changePercent { get; set; }
        public float iexMarketPercent { get; set; }
        public int iexVolume { get; set; }
        public int avgTotalVolume { get; set; }
        public float iexBidPrice { get; set; }
        public int iexBidSize { get; set; }
        public float iexAskPrice { get; set; }
        public int iexAskSize { get; set; }
        public long marketCap { get; set; }
        public float peRatio { get; set; }
        public float week52High { get; set; }
        public float week52Low { get; set; }
        public float ytdChange { get; set; }
    }
    

    然后按如下方式反序列化:

    var obj = JsonConvert.DeserializeObject<ToBeDeserialized>(json);
    

    输出:

    enter image description here

    更新:

    根据OP的评论,我使用了下面的另一个类来反序列化:

    public class ToBeDeserialized
    {
        public Quote quote { get; set; }
    }
    
    public class Quote
    {
        public string symbol { get; set; }
        public string companyName { get; set; }
        public string primaryExchange { get; set; }
        public string sector { get; set; }
        public string calculationPrice { get; set; }
        public float open { get; set; }
        public long openTime { get; set; }
        public float close { get; set; }
        public long closeTime { get; set; }
        public float high { get; set; }
        public float low { get; set; }
        public float latestPrice { get; set; }
        public string latestSource { get; set; }
        public string latestTime { get; set; }
        public long latestUpdate { get; set; }
        public int latestVolume { get; set; }
        public float iexRealtimePrice { get; set; }
        public int iexRealtimeSize { get; set; }
        public long iexLastUpdated { get; set; }
        public float delayedPrice { get; set; }
        public long delayedPriceTime { get; set; }
        public float previousClose { get; set; }
        public float change { get; set; }
        public float changePercent { get; set; }
        public float iexMarketPercent { get; set; }
        public int iexVolume { get; set; }
        public int avgTotalVolume { get; set; }
        public float iexBidPrice { get; set; }
        public int iexBidSize { get; set; }
        public float iexAskPrice { get; set; }
        public int iexAskSize { get; set; }
        public long marketCap { get; set; }
        public float peRatio { get; set; }
        public float week52High { get; set; }
        public float week52Low { get; set; }
        public float ytdChange { get; set; }
    }
    

    并获得一个引号列表,如:

    private static List<ToBeDeserialized> DeserializeAccordingly(string json)
    {
        dynamic data = JsonConvert.DeserializeObject(json);
        IDictionary<string, JToken> quotes = data;
        List<ToBeDeserialized> listOfQuote = new List<ToBeDeserialized>();
        foreach (var quote in quotes)
        {
            var qu = JsonConvert.DeserializeObject<ToBeDeserialized>(quote.Value.ToString());
            listOfQuote.Add(qu);
        }
        return listOfQuote;
    }