Home > Net >  C # Newtonsoft. Json serialization of byte type data
C # Newtonsoft. Json serialization of byte type data

Time:02-06

I use c # Newtonsoft. Json serialization Json data communicate with the server, but found that the serialization with Chinese utf8 string, became GBK code automatically, and then I direct serialized byte type character array, the serialized into base64 encoded automatically, and how to correct serialized as utf8 string,

 

Encoding utf8 Encoding of=. Utf8;
Encoding defalutEnc=Encoding. The Default;
Byte [] unicodeBytes=defalutEnc. GetBytes (" drops drop 123 ");//Chinese string
Byte [] asciiBytes=Encoding. The Convert (defalutEnc utf8, unicodeBytes);//utf8 byte array

Var sendText=utf8. Get string (asciiBytes);//utf8 string

Var data=https://bbs.csdn.net/topics/new
{
Type=5000,
The content=asciiBytes//1. Direct serialization byte
//the content=sendText//2. Serialization utf8 string
};

//directly serialized byte, dataStr inside the content into base64 encoded data
//serialization utf8 string, dataStr inside the content into GBK encoding data
Var dataStr=JsonConvert. SerializeObject (data);

//send json string
SendJsonStr (dataStr);



  •  Tags:  
  • C#
  • Related