. NET data type conversion method,. net data type
Using Newtonsoft. Json;
Using Newtonsoft. Json. Converters;
Using System. Web. Script. Serialization;
/// <Summary>
/// Convert string to dictionary type
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public Dictionary <string, int> Str2Dic (string value)
{
Dictionary <string, int> IDs = new Dictionary <string, int> ();
String [] str1 = value. Split (';');
For (int I = 0; I <str1.Length; I ++)
{
String [] str2 = str1 [I]. Split (',');
If (! IDs. ContainsKey (str2 [0]) & str2 [0]! = "")
{
IDs. Add (str2 [0], int. Parse (str2 [1]);
}
}
Return IDs;
}
Public Dictionary <string, string> StrToDic (string value)
{
Dictionary <string, string> list = new Dictionary <string, string> ();
String [] str1 = value. Split ('#');
For (int I = 0; I <str1.Length; I ++)
{
String [] str2 = str1 [I]. Split (';');
If (! List. ContainsKey (str2 [0]) & str2 [0]! = "")
{
List. Add (str2 [0], str2 [1]);
}
}
Return list;
}
// Convert dictionary to string
Public string DicToStr (Dictionary <string, string> list)
{
String result = "";
Foreach (KeyValuePair <string, string> l in list)
{
Result + = l. Key + ";" + l. Value + "#";
}
Return result;
}
// List to string
Public string ListToStr (List <string> list)
{
String result = "";
Foreach (string s in list)
{
Result + = s + ",";
}
Return result;
}
Public List <string> StrToList (string str)
{
List <string> list = new List <string> ();
String [] s = str. Split (',');
For (int I = 0; I <s. Length; I ++)
{
If (s [I]! = ""&&! List. Contains (s [I])
{
List. Add (s [I]);
}
}
Return list;
}