C#: how to get all values for keys from JS Object?

SentineL

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];
   }
}
Troy Gizzi

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);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get all properties values of a JavaScript Object (without knowing the keys)?

From Dev

How to get all keys from dictionary that specified values?

From Dev

how to get keys and values from a Map(obj) in a loop (js)?

From Dev

Native js or Underscore, get all values from an object and put into string

From Dev

Get all Keys:Values from Chrome Storage

From Dev

Get all subnode keys and values from zookeeper

From Dev

How to get all keys from the cache in Apache Ignite C++

From Dev

How to get all values of specific nested property in JS object

From Dev

How to get specific object values from all child location in firebase

From Dev

How to get values from keys from JSON

From Dev

JavaScript: How can I get all the keys and values of an object that begin with a specific string?

From Dev

how to sum up all values with same keys in an object inside an array and get their average

From Dev

how to sum up all values with same keys in an object inside an array and get their average

From Dev

How to get all values from Python tuple keys from dictionaries into one list?

From Dev

How to get all values from list created by immutable.js?

From Dev

Get a sum of all values that have matching keys in a JavaScript Object?

From Dev

How to get values from string by matching keys?

From Dev

How to get all keys from Ordered Dictionary?

From Dev

How to get all keys and values into separate String arrays from NSDictionary in Swift?

From Dev

Python How can I get all values same name keys from dictionary

From Dev

How to get all values from guava LoadingCache without passing any keys

From Dev

how to get the values for all matching keys from JSON in Java and Jackson parser

From Dev

Get all string values from a nested object

From Dev

How to get object values in a specified manner by with keys and values

From Dev

How to get all the values from the same Name xml c#

From Dev

how get values from object to local variable c#

From Dev

How to get all values from all textfields

From Dev

How to get all values from all textfields

From Dev

how to check if all object keys has false values

Related Related

  1. 1

    How to get all properties values of a JavaScript Object (without knowing the keys)?

  2. 2

    How to get all keys from dictionary that specified values?

  3. 3

    how to get keys and values from a Map(obj) in a loop (js)?

  4. 4

    Native js or Underscore, get all values from an object and put into string

  5. 5

    Get all Keys:Values from Chrome Storage

  6. 6

    Get all subnode keys and values from zookeeper

  7. 7

    How to get all keys from the cache in Apache Ignite C++

  8. 8

    How to get all values of specific nested property in JS object

  9. 9

    How to get specific object values from all child location in firebase

  10. 10

    How to get values from keys from JSON

  11. 11

    JavaScript: How can I get all the keys and values of an object that begin with a specific string?

  12. 12

    how to sum up all values with same keys in an object inside an array and get their average

  13. 13

    how to sum up all values with same keys in an object inside an array and get their average

  14. 14

    How to get all values from Python tuple keys from dictionaries into one list?

  15. 15

    How to get all values from list created by immutable.js?

  16. 16

    Get a sum of all values that have matching keys in a JavaScript Object?

  17. 17

    How to get values from string by matching keys?

  18. 18

    How to get all keys from Ordered Dictionary?

  19. 19

    How to get all keys and values into separate String arrays from NSDictionary in Swift?

  20. 20

    Python How can I get all values same name keys from dictionary

  21. 21

    How to get all values from guava LoadingCache without passing any keys

  22. 22

    how to get the values for all matching keys from JSON in Java and Jackson parser

  23. 23

    Get all string values from a nested object

  24. 24

    How to get object values in a specified manner by with keys and values

  25. 25

    How to get all the values from the same Name xml c#

  26. 26

    how get values from object to local variable c#

  27. 27

    How to get all values from all textfields

  28. 28

    How to get all values from all textfields

  29. 29

    how to check if all object keys has false values

HotTag

Archive