Is there anyway to handy convert a dictionary to String?

后端 未结 12 1245
死守一世寂寞
死守一世寂寞 2020-12-13 23:50

I found the default implemtation of ToString in the dictionary is not what I want. I would like to have {key=value, ***}.

Any handy way to get it?

12条回答
  •  臣服心动
    2020-12-14 00:04

    I got this simple answer.. Use JavaScriptSerializer Class for this.

    And you can simply call Serialize method with Dictionary object as argument.

    Example:

    var dct = new Dictionary();
    var js = new JavaScriptSerializer();
    dct.Add("sam","shekhar");
    dct.Add("sam1","shekhar");
    dct.Add("sam3","shekhar");
    dct.Add("sam4","shekhar");
    Console.WriteLine(js.Serialize(dct));
    

    Output:

    {"sam":"shekhar","sam1":"shekhar","sam3":"shekhar","sam4":"shekhar"}
    

提交回复
热议问题