jsoncpp解析u编码中文【源码修改】

  • 本帖最后由 u3dtest 于 2012-4-16 15:49 编辑 *

好吧,这是个很老的问题了,网上的解决方法也很多,之前因为jsoncpp解析u中文解析不出来挠过各种墙,再次分享出来给大家用。
打开json_reader.cpp,再Reader::decodeString( Token &token, std::string &decoded )方法上面添加转码方法,如下:

static inline void appendUnicodeToUtf8(unsigned int c, std::string &utf8) {
if (c < 0x00080) {
utf8 += static_cast(c & 0xFF);
} else if (c < 0x00800) {
utf8 += static_cast(0xC0 + ((c >> 6) & 0x1F));
utf8 += static_cast(0x80 + (c & 0x3F));
} else if (c < 0x10000) {
utf8 += static_cast(0xE0 + ((c >> 12) & 0x0F));
utf8 += static_cast(0x80 + ((c >> 6) & 0x3F));
utf8 += static_cast(0x80 + (c & 0x3F));
} else {
utf8 += static_cast(0xF0 + ((c >> 18) & 0x07));
utf8 += static_cast(0x80 + ((c >> 12) & 0x3F));
utf8 += static_cast(0x80 + ((c >> 6) & 0x3F));
utf8 += static_cast(0x80 + (c & 0x3F));
}
}

然后在Reader::decodeString( Token &token, std::string &decoded )方法修改为
if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) )
return false;
// @todo encode unicode as utf8.
appendUnicodeToUtf8(unicode, decoded);//开始转码
// @todo remember to alter the writer too.
}

OK,搞定了,再解析u编码时不用挠墙了

附件是jsoncpp修改够的源码
48

顶~~~~~~~

谢谢分享:)

附件在哪里啊