如何将 pusher.com 的“序列化”json 解析为 qt5 QJsonDocument?
Posted
技术标签:
【中文标题】如何将 pusher.com 的“序列化”json 解析为 qt5 QJsonDocument?【英文标题】:How to parse pusher.com's "serialized" json into qt5 QJsonDocument? 【发布时间】:2015-03-03 23:35:15 【问题描述】:我从我的 qt5 应用程序中的 pusher.com WebSocket 获取一些 json 数据,我正在努力解析他们发送的“序列化”json。我终于确定了问题,但我不知道如何解决它:
我做了一个小测试程序来演示:
QString str1 = "\"event\":\"message\",\"data\":\"\\\"channel\\\":\\\"trade.3\\\",\\\"trade\\\":\\\"tradeid\\\":\\\"86364698\\\",\\\"timestamp\\\":1425420405,\\\"datetime\\\":\\\"2015-03-03 17:06:45 EDT\\\",\\\"marketid\\\":\\\"3\\\",\\\"marketname\\\":\\\"LTC\\\\/BTC\\\",\\\"quantity\\\":\\\"0.24680554\\\",\\\"price\\\":\\\"0.00694988\\\",\\\"total\\\":\\\"0.00171527\\\",\\\"type\\\":\\\"Buy\\\"\",\"channel\":\"trade.3\"";
QString str2="\"event\":\"message\",\"data\": \"channel\":\"trade.3\",\"trade\":\"tradeid\":\"86364698\",\"timestamp\":1425420405,\"datetime\":\"2015-03-03 17:06:45 EDT\",\"marketid\":\"3\",\"marketname\":\"LTC\\/BTC\",\"quantity\":\"0.24680554\",\"price\":\"0.00694988\",\"total\":\"0.00171527\",\"type\":\"Buy\",\"channel\":\"trade.3\"";
QJsonDocument doc1 = QJsonDocument::fromJson(str1.toUtf8());
QJsonDocument doc2 = QJsonDocument::fromJson(str2.toUtf8());
qDebug("%s", qPrintable(QString("Str1: \"%1\"").arg(str1)));
qDebug("%s", qPrintable(QString(doc1.toJson())));
qDebug() << "-----------------------------------------------------------";
qDebug("%s", qPrintable(QString("Str2: \"%1\"").arg(str2)));
qDebug("%s", qPrintable(QString(doc2.toJson())));
这个输出:
Str1: ""event":"message","data":"\"channel\":\"trade.3\",\"trade\":\"tradeid\":\"86364698\",\"timestamp\":1425420405,\"datetime\":\"2015-03-03 17:06:45 EDT\",\"marketid\":\"3\",\"marketname\":\"LTC\\/BTC\",\"quantity\":\"0.24680554\",\"price\":\"0.00694988\",\"total\":\"0.00171527\",\"type\":\"Buy\"","channel":"trade.3""
"channel": "trade.3",
"data": "\"channel\":\"trade.3\",\"trade\":\"tradeid\":\"86364698\",\"timestamp\":1425420405,\"datetime\":\"2015-03-03 17:06:45 EDT\",\"marketid\":\"3\",\"marketname\":\"LTC\\/BTC\",\"quantity\":\"0.24680554\",\"price\":\"0.00694988\",\"total\":\"0.00171527\",\"type\":\"Buy\"",
"event": "message"
-----------------------------------------------------------
Str2: ""event":"message","data": "channel":"trade.3","trade": "tradeid":"86364698","timestamp":1425420405,"datetime":"2015-03-03 17:06:45 EDT","marketid":"3","marketname":"LTC\/BTC","quantity":"0.24680554","price":"0.00694988","total":"0.00171527","type":"Buy","channel":"trade.3""
"channel": "trade.3",
"data":
"channel": "trade.3",
"trade":
"datetime": "2015-03-03 17:06:45 EDT",
"marketid": "3",
"marketname": "LTC/BTC",
"price": "0.00694988",
"quantity": "0.24680554",
"timestamp": 1425420405,
"total": "0.00171527",
"tradeid": "86364698",
"type": "Buy"
,
"event": "message"
显然第二个版本是我的目标。有没有办法轻松转换输入数据,还是我必须尝试推出自己的转换器功能?
编辑:以下是这个特定数据样本的诀窍......但我仍然认为必须有一种更合适的方法来转换这种东西......
str1.replace("\\\"", "\"");
str1.replace("\\\\", "\\");
str1.replace("\"", "");
str1.replace("\"", "");
【问题讨论】:
【参考方案1】:您的“数据”实际上是另一个 JSON 文档。
只需从 doc1.object()["data"].toString() 创建另一个 QJsonDocument;
但很明显“数据”可以有其他东西,在这个例子中它是一个 json 文档。
祝你好运!
【讨论】:
以上是关于如何将 pusher.com 的“序列化”json 解析为 qt5 QJsonDocument?的主要内容,如果未能解决你的问题,请参考以下文章