作者:朱金灿
来源:http://blog.csdn.net/clever101
最近使用qt(我用的是qt5.6)解析json字符串,发现qt的一个bug,代码如下:
#include
#include
#include
#include
#include
#include
#include
#include "json.h"
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
int QtJsonTest()
{
QJsonParseError jsonError;
QString qstrJson = "{\n \"result\": true,\n \"error\": \"\",\n \"data\": {\n \"User\": {\n \"Uid\": 20556426650023957,\n \"Username\": \"00B10C201D61\",\n \"Password\": \"e10adc3949ba59abbe56e057f20f883e\",\n \"Appid\": 9,\n \"Sex\": \"\",\n \"Email\": \"\",\n \"GroupId\": 0,\n \"ParentId\": 0,\n \"Status\": \"\",\n \"Registertime\": \"2017-11-10T10:35:38+08:00\",\n \"Nikename\": \"\",\n \"TelNumber\": \"\",\n \"Lastlogintime\": \"2017-11-10T19:37:28.305962078+08:00\",\n \"Role\": \"\",\n \"Address\": \"\",\n \"IconPath\": \"\",\n \"ImagePath\": \"\",\n \"Type\": 0,\n \"Admin\": false,\n \"Option\": \"\",\n \"Terminals\": null\n },\n \"sessionid\": \"62719fc6cd0fc53a5dad5fed77ebfbd3\",\n \"pushsessionid\": \"9_20556426650023957_4de289715bed48e21c919b48febd7ae2\"\n }\n}";
QJsonDocument json = QJsonDocument::fromJson(qstrJson.toUtf8(), &jsonError);
if (jsonError.error == QJsonParseError::NoError)
{
if (json.isObject())
{
QJsonObject rootObj = json.object();
if (rootObj.contains("data"))
{
QJsonValue data_value = rootObj.value(QString("data"));
QJsonObject contentObj = data_value.toObject();
if (contentObj.contains("User"))
{
QJsonValue user_value = contentObj.value(QString("User"));
QJsonObject userObj = user_value.toObject();
if (userObj.contains("Uid"))
{
QJsonValue UidValue = userObj.value("Uid");
QJsonValue::Type type = UidValue.type();
if (UidValue.isDouble())
{
double db = UidValue.toDouble();
QString info("QT解析json字符串结果:");
std::wcout << info.toStdWString()<< std::fixed << db << std::endl;
return 0;
}
}
}
}
}
}
return 1;
}
int JsoncppTest()
{
QString qstrJson = "{\n \"result\": true,\n \"error\": \"\",\n \"data\": {\n \"User\": {\n \"Uid\": 20556426650023957,\n \"Username\": \"00B10C201D61\",\n \"Password\": \"e10adc3949ba59abbe56e057f20f883e\",\n \"Appid\": 9,\n \"Sex\": \"\",\n \"Email\": \"\",\n \"GroupId\": 0,\n \"ParentId\": 0,\n \"Status\": \"\",\n \"Registertime\": \"2017-11-10T10:35:38+08:00\",\n \"Nikename\": \"\",\n \"TelNumber\": \"\",\n \"Lastlogintime\": \"2017-11-10T19:37:28.305962078+08:00\",\n \"Role\": \"\",\n \"Address\": \"\",\n \"IconPath\": \"\",\n \"ImagePath\": \"\",\n \"Type\": 0,\n \"Admin\": false,\n \"Option\": \"\",\n \"Terminals\": null\n },\n \"sessionid\": \"62719fc6cd0fc53a5dad5fed77ebfbd3\",\n \"pushsessionid\": \"9_20556426650023957_4de289715bed48e21c919b48febd7ae2\"\n }\n}";
Json::Reader reader;
Json::Value root;
if (reader.parse(qstrJson.toStdString(), root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
qint64 uid = root["data"]["User"]["Uid"].asInt64(); // 访问节点
QString info("Jsoncpp库解析json字符串结果:");
std::wcout << info.toStdWString() << uid << std::endl;
return 0;
}
else
{
return 1;
}
return 1;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
setlocale(LC_CTYPE, "chs");
QtJsonTest();
JsoncppTest();
getchar();
return 1;
}
代码编译环境为VS2013,Win7操作系统,unicode字符集,输出结果如下:
从上图可以看出jsoncpp的解析精度比qt5要高。通过查看qt源码也可以看出,qt对json类型的支持比较有限,仅支持以下类型:
enum Type {
Null = 0x0,
Bool = 0x1,
Double = 0x2,
String = 0x3,
Array = 0x4,
Object = 0x5,
Undefined = 0x80
};
jsoncpp支持json的类型比qt要多,具体是:
enum ValueType {
nullValue = 0, ///< 'null' value
intValue, ///< signed integer value
uintValue, ///< unsigned integer value
realValue, ///< double value
stringValue, ///< UTF-8 string value
booleanValue, ///< bool value
arrayValue, ///< array value (ordered list)
objectValue ///< object value (collection of name/value pairs).
};