Jsoncpp decoding code Chinese is empty garbled problem

Source: Internet
Author: User

In this, only to their own problems to make a summary, did not expect to help everyone.

Local C + + Desktop program, using Jsoncpp to the JSON and server communication, static library compilation can not be used, so the use of the source copy to call

The server communicates with PHP and the client

Service-side JSON decoding and encoding of two functions Json_encode Json_decode

If there is Chinese in the string used in the use of Json_encode, it may appear, after encoding, the string is empty,

One of the reasons I met is that the PHP script file type is ANSI instead of UTF8, so use the TXT text editor, save the script as UTF8 can, if not solve the problem, can only detour to find other reasons and methods.

In other words, the client C + + desktop application, using Jsoncpp and server communication, when uploading JSON data, the JSON will always be incorrect in Chinese, either garbled or no value.

As if I have read an article before, I can't remember, here is a word, right see you understand, wrong, please point out, or will not be misleading on the line. That's jsoncpp. Only ANSI-encoded string data formats are supported

May be let me blind cat hit Dead mouse, and toss one hours of problems, with this solution.

My compilation environment vs2015 Win7 Platform coded character set is Unicode

Upload with Jsoncpp encoding always error, with tool function Unicodetoansi A turn, then transfer, there is no problem. The following stickers, a few commonly used tool functions, recommended encapsulation into a tool class, function declaration as a static function, through the class name:: Function name directly call

#include"cutil.h"#include<Windows.h>wstring Cutil::utf8tounicode (Const string&str) {    intLen =0; Len=str.length (); intUnicodelen =:: MultiByteToWideChar (Cp_utf8,0, Str.c_str (),-1, NULL,0); wchar_t*Punicode; Punicode=NewWchar_t[unicodelen +1]; memset (Punicode,0, (Unicodelen +1)*sizeof(wchar_t)); :: MultiByteToWideChar (Cp_utf8,0, Str.c_str (),-1, (LPWSTR) Punicode, Unicodelen);    wstring RT; RT= (wchar_t*) Punicode; DeletePunicode; returnRT;}stringCutil::unicodetoutf8 (Constwstring&str) {    Char*Pelementtext; intItextlen; //wide char to multi charItextlen =WideCharToMultiByte (Cp_utf8,0, Str.c_str (),-1, NULL,0, NULL, NULL); Pelementtext=New Char[Itextlen +1]; memset ((void*) Pelementtext,0,sizeof(Char) * (Itextlen +1)); :: WideCharToMultiByte (Cp_utf8,0, Str.c_str (),-1, Pelementtext, Itextlen, NULL, and NULL); stringStrText; StrText=Pelementtext; Delete[] pelementtext; returnStrText;} Wstring Cutil::ansitounicode (Const string&str) {    intLen =0; Len=str.length (); intUnicodelen =:: MultiByteToWideChar (CP_ACP,0, Str.c_str (),-1, NULL,0); wchar_t*Punicode; Punicode=NewWchar_t[unicodelen +1]; memset (Punicode,0, (Unicodelen +1)*sizeof(wchar_t)); :: MultiByteToWideChar (CP_ACP,0, Str.c_str (),-1, (LPWSTR) Punicode, Unicodelen);    wstring RT; RT= (wchar_t*) Punicode; DeletePunicode; returnRT;}stringCutil::unicodetoansi (Constwstring&str) {    Char*Pelementtext; intItextlen; //wide char to multi charItextlen =WideCharToMultiByte (CP_ACP,0, Str.c_str (),-1, NULL,0, NULL, NULL); Pelementtext=New Char[Itextlen +1]; memset ((void*) Pelementtext,0,sizeof(Char) * (Itextlen +1)); :: WideCharToMultiByte (CP_ACP,0, Str.c_str (),-1, Pelementtext, Itextlen, NULL, and NULL); stringStrText; StrText=Pelementtext; Delete[] pelementtext; returnStrText;}

Called in code

String str = Cutil::unicodetoansi (Client->getname (). GetBuffer (0));

login["name"] = str;

To this, my problem solved, welcome message discussion, always have different problems, there are always different ways to deal with.

I am more lazy, the principle of things made a little vague.

Only under the painstaking effort, can acquire the real work!

Jsoncpp decoding code Chinese is empty garbled problem

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.