简体   繁体   中英

Iterating over properties of an object in Script#

What Script# code 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM