简体   繁体   English

将 javascript object 转换为相应的 C# 字典所需的帮助

[英]Help needed to convert a javascript object to corresponding C# dictionary

Hi I'm kind of newbie in C#, I have this javascript code that I need to convert into corresponding c# dictionary嗨,我是 C# 的新手,我有这个 javascript 代码,我需要将其转换为相应的 c# 字典

given below is the javascript code下面给出的是 javascript 代码

 EXCHANGES = {
        "ICE": {
            source: "SPC",
            name: "ICE Futures Europe",
            assetTypes: {
                CER: {
                    spot: "ECX CER DAILY FUTURE",
                    fut: "ECX CER FUTURE"
                },
                EUA: {
                    spot: "ECX EUA DAILY FUTURE",
                    fut: "ECX EUA FUTURE"
                }
            }
        },
        "CLIMEX": {
            source: "CLX",
            name: "Climex Spot Market",
            symbols: {
                CER: {
                    spot: ["CER"]
                },
                EUA: {
                    spot: ["EUA 08-12"]
                }
            }
        },
        "BLUENEXT": {
            source: "BLNXT",
            name: "Bluenext",
            symbols: {
                CER: {
                    spot: ["1000019-82-1"]
                },
                EUA: {
                    spot: ["1000019-81-1"]
                }
            }
        }
    };

So far what I have manage is this到目前为止我所管理的是这个

public Dictionary<string, Dictionary<string, string>> Exchanges = new Dictionary<string, Dictionary<string, string>> {
            {"ICE ECX", new Dictionary<string, string> {
                {"source", "SPC"},
                {"name", "ICE Futures Europe"},
                {"exchange", "ICE" }
            }},
            {"Climex", new Dictionary<string, string> {
                {"source", "CLX"},
                {"name", "Climex Spot Market"},
                {"exchange", "Climex" }
            }},
            {"BlueNext", new Dictionary<string, string> {
                {"source", "BLNXT"},
                {"name", "Bluenext"},
                {"exchange", "BlueNext" }
            }},
            {"Green Exchange", new Dictionary<string, string> {
                {"source", "SPC"},
                {"name", "NYMEX: Futures"},
                {"exchange", "NYMEX" }
            }},
            {"NYMEX_FA", new Dictionary<string, string> {
                {"source", "SPC"},
                {"name", "NYMEX: Futures Acess"},
                {"exchange", "NYMEX_FA" }
            }}
        };

Can anyone of you can guide me to the correct way to do this any help will be appreciated.你们中的任何人都可以指导我正确的方法来做到这一点,任何帮助将不胜感激。 thanks Pranay谢谢普拉奈

I'd use a JSON Parser.我会使用 JSON 解析器。 Like this .这样
In that way you can retrieve the properties and values after parsing from the JObject .这样,您可以在从JObject解析后检索属性和值。

string jsonText = @"{
  prop1: 1, 
  prop2: 'Some String',
  'prop3': {
    iProp1: 1,
    iProp2: 2
}";
JObject parsedJson = JObject.Parse(jsonText);

I'd give a try to ExpandoObject , which is dynamic and implements IDictionary<string,object>.我会尝试ExpandoObject ,它是动态的并实现IDictionary<string,object>。 For instance, you can write the following code:例如,您可以编写以下代码:

dynamic x = new ElasticObject();
x.Ice = new ExpandoObject();
x.Ice.Source = "SPC";
x.Ice.Name = "Ice Futures Europe";
x.AssetTypes = new ExpandoObject();
x.AssetTypes.CER = new ExpandoObject();
x.AssetTypes.CER.Spot = "EXC CER DAILY FUTURE";
x.AssetTypes.CER.Fut = "EXC CER FUTURE";
x.EUA = new ExpandoObject();
x.EUA.Spot = "ECX EUA DAILY FUTURE";
x.EUA.Fut = "ECX EUA FUTURE";

On MSDN you can read more about ExpandoObject here .在 MSDN 上,您可以 在此处阅读有关 ExpandoObject 的更多信息。

If you need a yet smarter object, there is the ElasticObject (described here ).如果您需要更智能的 object,可以使用ElasticObject在此处描述)。 Using it you can skip the declaration of the nested Expandos, like this:使用它,您可以跳过嵌套 Expandos 的声明,如下所示:

dynamic x = new ExpandoObject();
x.Ice.Source = "SPC";
x.Ice.Name = "Ice Futures Europe";
x.AssetTypes.CER.Spot = "EXC CER DAILY FUTURE";
x.AssetTypes.CER.Fut = "EXC CER FUTURE";
x.EUA.Spot = "ECX EUA DAILY FUTURE";
x.EUA.Fut = "ECX EUA FUTURE";

Hope this helps.希望这可以帮助。

This what I did basically I created classes since maintaining this level of dictionary would have been cumbersome这就是我所做的,基本上我创建了类,因为维护这个级别的字典会很麻烦

public class Exchange
        {
            public string Source {get; set;}
            public string Name {get; set;}
            public Dictionary<string, AssetTypeSymbol> AssetTypes {get; set;}
            public Dictionary <string,AssetTypeSymbol>Symbols {get; set;}
        }
        public class AssetTypeSymbol
        {
            public IList<string> Spot {get; set;}
            public IList<string> Fut {get; set;}
        }

    public Dictionary<string,Exchange> Exchanges = new Dictionary<string,Exchange>
    {
            {"ICE ECX", new Exchange() {
                Source = "SPC",
                Name = "ICE Futures Europe",
                AssetTypes = new Dictionary<string, AssetTypeSymbol>() {
                    {"CER", new AssetTypeSymbol() {
                        Spot = new string[] { "ECX CER DAILY FUTURE" },
                        Fut = new string[] { "ECX CER FUTURE" }
                    }},
                    .......... and so on 

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

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