开发者

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

given below is the javascript code

 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: {
                C开发者_如何转开发ER: {
                    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. Like this.
In that way you can retrieve the properties and values after parsing from the 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>. 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.

If you need a yet smarter object, there is the ElasticObject (described here). Using it you can skip the declaration of the nested Expandos, like this:

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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜