开发者

Iterating over properties of an object in Script#

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜