开发者

Iterating over properties of an object in Script#

开发者 https://www.devze.com 2023-03-12 09:10 出处:网络
What Script# 开发者_如何学JAVAcode would generate the following JavaScript? var obj = eval(\'(\' + jsonText + \')\');

What Script# 开发者_如何学JAVAcode would generate the following JavaScript?

var obj = eval('(' + jsonText + ')');

for (key in obj)  // what C# code translates to this iteration?
{
    // ...
}

thanks.


You can come close with:

Object obj = Script.Eval("(" + json + ")");
foreach (DictionaryEntry entry in Dictionary.GetDictionary(obj))
{

}

which generates (in Script# 0.7.2):

var obj = eval('(' + json + ')');
var $dict1 = obj;
for (var $key2 in $dict1) {
    var entry = { key: $key2, value: $dict1[$key2] };
}

Side Note: There is a binding in Script# already for native JSON. You could replace Script.Eval(...) with Json.Parse(...) in namespace System.Serialization if you are targeting browsers with native JSON support or will be including the popular json2.js library or the like.

0

精彩评论

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