. Net JSON dictionary serialization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.