I am working on a project these two days. net Communication, which is unpleasant. net Dictionary (idictionary) will be converted into a list of key-value pairs during serialization, rather than the most common JSON object. Datacontractjsonserializer converts each key-value pair into {"key": "Age", "value": 10}. What I want is "Age": 10. I searched the internet for a long time, and there was no good way. I thought of open source, and went to fastjson to play. I found the same thing, but it turned into {"K": "Age", "V ": 10}, still uncomfortable. Finally, I looked at the source code and changed the writedictionary method of the class jsonserializer related to dictionary serialization to the following format. Finally, I am OK:
Private Void Writedictionary (idictionary DIC)
{
_ Output. append ( " { " );
Bool Pendingseparator = False ;
Foreach (Dictionaryentry entry In DIC)
{
If (Pendingseparator)
_ Output. append ( " , " );
Writepair (entry. Key. tostring (), entry. value );
Pendingseparator = True ;
}
_ Output. append ( " } " );
// _ Output. append ("[");
// Bool pendingseparator = false;
// Foreach (dictionaryentry entry in DIC)
// {
// If (pendingseparator)
// _ Output. append (",");
// _ Output. append ("{");
// Writepair ("K", entry. Key );
// _ Output. append (",");
// Writepair ("V", entry. value );
// _ Output. append ("}");
// Pendingseparator = true;
// }
// _ Output. append ("]");
}
Below is the testCode:
Static Void Main ( String [] ARGs)
{
VaR DIC = New DIC ();
Dic [ " No " ] = 10 ;
VaR subdic = New DIC ();
Subdic [ " Val " ] = 12.3 ;
Dic [ " Detail " ] = Subdic;
VaR Str = JSON. instance. tojson (DIC );
Console. writeline (STR );
}
Public Class LST: arraylist {}
Public Class Dic: hashtable {}
Fastjson can be found below:
Http://www.codeproject.com/KB/IP/fastJSON.aspx