1

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: {
                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

1
  • 1
    Do you have .NET 4.0? This can make your conversion a bit easier because of the ExpandoObject. Commented Jul 14, 2011 at 16:46

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This is indeed the easiest way. Also, be sure to reference the deserialized obect as an Expandoobject as ChaosPandion correctly observed. This will save you from having to use reflection.
0

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.

1 Comment

Thanx Hemme for your solution but unfortunately ExpandoObject works only in C# 4.0 and I'm still stuck in C#2.0
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.