简体   繁体   English

如何反序列化包含列表和字典(或键值)属性对象的json字符串

[英]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. 我想通过解析包含嵌入式contianer对象的Json字符串来创建C#对象。 See StockHistory in the code snippet. 请参见代码段中的StockHistory。

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. 就像使用“字典”一样在列表中包装和不包装“ KeyValue属性”一样,也尝试使用单引号或双引号来定义字符串。 Most Stackoverflow listings shows a list or dictionary as the parent object (contianer). 大多数Stackoverflow清单都将列表或词典显示为父对象(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. 我无法更改Json…它来自第三方服务器。

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. 当我使用Visual Studio 2017 JSON Visualizer检查json字符串时(通过突出显示变量并单击放大草),一切看起来都很好并且与预期的一样。

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)". 我希望c#变量“ a”(StockHistory)包含已解析的JSON密钥和JSON字段“元数据”和“时间序列(5分钟)”的数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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