我该如何正确地反序列化这个对象?

时间:2018-03-02 18:30:27

标签: c# json json.net

这可能很容易,提前道歉。我只是对它感到沮丧,而且我的JSON容忍度依赖于当天。

该字符串反序列化密钥,但当Quote对象尝试反序列化时,没有运气。构造Quote对象但是所有值都为null /默认为其对象的null值(0表示int / double,null表示字符串等)。 “quote”属性未被反序列化为对象。

我试过

JsonConvert.DeserializeObject<Dictionary<string, Quote>>

“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 个答案:

答案 0 :(得分:1)

因为您必须使用不同的对象,AMAT&amp; 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

UPDATE:

根据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;
}