将javascript对象转换为相应的c#字典所需的帮助
本文关键字:字典 帮助 对象 javascript 转换 | 更新日期: 2023-09-27 18:02:01
嗨,我是c#新手,我有一段javascript代码需要转换成相应的c#字典
下面给出的是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"]
}
}
}
};
到目前为止我管理的是这个
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" }
}}
};
你们中有谁能指导我正确的方法来做这件事,任何帮助都会很感激。谢谢Pranay
我会使用JSON解析器。这样的。
通过这种方式,您可以在解析JObject
后检索属性和值。
string jsonText = @"{
prop1: 1,
prop2: 'Some String',
'prop3': {
iProp1: 1,
iProp2: 2
}";
JObject parsedJson = JObject.Parse(jsonText);
我会尝试ExpandoObject,这是动态的并实现 dictionary<string,object>。例如,您可以编写以下代码:
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";
在MSDN上,你可以在这里阅读更多关于ExpandoObject的信息。如果你需要一个更智能的对象,有ElasticObject(在这里描述)。使用它可以跳过嵌套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";
我所做的基本上就是创建类,因为维护这种级别的字典会很麻烦
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