Announcement

Collapse
No announcement yet.

JSON Strigify

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSON Strigify

    I want to pass a Dictionary into a function which accepts object, and stringify if.
    public function string makeJson(object o) {
        if (o is IDictionary) {
        /// what to do here ?  How to extract keys and values ? 
        }
    }
    Thanks in advance

  • #2
    Hi rumc,

    I'm not entirely sure I understand your requirements. Can you provide simplified Deck or DotNetFiddle demonstrating as best you can what you require?

    The new Bridge.Newtonsoft.Json library might be helpful too.

    Comment


    • #3
      My solution was the following:
      https://deck.net/1bf073e6710b997a19f6ec664b98d1fc
      public class Program
      {
          public static void Main()
          {
              var o = new Dictionary< string, string >();
              o["asd"] = "1";
              o["qwe"] = "2";
              Test(o);
          }
      
          public static void Test(object o) {
              if (o is IDictionary) {
              /// How to extract keys and values ? 
              // here is my way:
              var dict = o.As< Dictionary< object, object > >();
              foreach (var item in dict) {
                  Console.WriteLine("key={0}, value={1}", item.Key, item.Value);
              }
          }
      }
      }
      Is there any better way to check object is a dictionary and get keys and values ?

      Comment

      Working...
      X