Reputation: 4732
I have a C# application in web page. Web calls my apllication via JS:
var data = {key1:value1, key2:value2};
app["methodName"](data);
So, in my app:
public void methodName(object data)
{
//need here something like this:
foreach (var key in data)
{
var value = data[key];
}
}
Upvotes: 0
Views: 931
Reputation: 17194
Like this ?
var data = {'key1':'value1', 'key2':'value2'};
var arr = $.map(data,function(k,v){
return k;
});
document.body.innerHTML = arr;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Here, arr
will have all the values.
Upvotes: 0
Reputation: 2519
The Newtonsoft.JSON library lets you parse arbitrary JSON into a dynamic object like so:
using Newtonsoft.Json.Linq;
...
dynamic obj = JObject.Parse("{\"key1\":\"value1\", \"key2\":\"value2\"}");
Console.WriteLine(obj.key1);
Console.WriteLine(obj.key2);
The equivalent of the method you provided in your post would be similar to this:
public static void methodName(dynamic data)
{
foreach (var keyValuePair in data)
{
var value = keyValuePair.Value;
Console.WriteLine(keyValuePair.Name + ": " + keyValuePair.Value);
}
}
Upvotes: 3