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.
精彩评论