简体   繁体   中英

How can I deserialize a json string contianing a list and dictionary (or keyvalue) property objects

I want to create a C# object by parsing a Json string that contains embedded contianer objects. See StockHistory in the code snippet.

I tried defining the parent object in many ways the compiler would allowed. Like wrapping and not wrapping "KeyValue property" in a List, using a Dictionary, also, tring to defining the strings with single vs double quotation marks. Most Stackoverflow listings shows a list or dictionary as the parent object (contianer). But my containers are embedded.

However, if a custom converter is needed... I'm not sure on how to write it. A code snippet would be great. I can not change the Json… it's coming from a third-party server.

When I examine the json string with the Visual Studio 2017 JSON Visualizer (by highlighting the variable and clicking on the magnify grass) all appears good and as expected.

using Newtonsoft.Json; // I'm using NuGet vs: 12.0.0.0
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        public class StockHistory
        {
            public List<string> Meta_Data { get; set; }
            public Dictionary<string, LHOCV> Lhocv { get; set; }
        }

        public class LHOCV
        {
            public float Low { get; set; }
            public float High { get; set; }
            public float Open { get; set; }
            public float Close { get; set; }
            public double Volume { get; set; }
        }

        static void Main(string[] args)
        {
            string json =
 @"{
    'Meta Data': {
        '1. Information': 'Intraday (5min) ...',
        '2. Symbol': 'MSFT',
        '3. Last Refreshed': '2019-01-22 16:00:00',
        '4. Interval': '5min',
        '5. Output Size': 'Full size',
        '6. Time Zone': 'US/Eastern'
    },
    'Time Series (5min)': {
        '2019-01-22 16:00:00': {
            '1. open': '105.2200',
            '2. high': '105.8700',
            '3. low': '105.1000',
            '4. close': '105.8200',
            '5. volume': '1619877'
        },
        '2019-01-22 15:50:00': {
            '1. open': '105.4200',
            '2. high': '105.4800',
            '3. low': '105.2600',
            '4. close': '105.3000',
            '5. volume': '452625'
        }
    }
}";

StockHistory a = JsonConvert.DeserializeObject<StockHistory>(json);
// I can not get a value assigned to "a"... both container objects,
// Meta_Data and Lhocv, are rendered null.

//Console.WriteLine(a.Lhocv["2019-01-22 16:00:00"].High);
        }

    }
}

I expected the c# variable "a" (StockHistory), to contain the parsed JSON Keys and data for JSON fields "Meta Data" and "Time Series (5min)".

What I get are null values for the containers.

This might get you close:

public class StockHistory
{
    [JsonProperty("Meta Data")]
    public Dictionary<string, string> Meta_Data { get; set; }
    [JsonProperty("Time Series (5min)")]
    public Dictionary<string, LHOCV> Lhocv { get; set; }
}

public class LHOCV
{
    [JsonProperty("3. low")]
    public float Low { get; set; }
    [JsonProperty("2. high")]
    public float High { get; set; }
    [JsonProperty("1. open")]
    public float Open { get; set; }
    [JsonProperty("4. close")]
    public float Close { get; set; }
    [JsonProperty("5. volume")]
    public double Volume { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM