Qjson 获取对象名称
Qjson get object name
我有一个json对象,如何获取对象名称?我不能有对象名称,它将由服务器发送,这样我应该得到对象名称。
{
"success":1,
"return":{
"343152":{//get this object name
"pair":"usd_eur",
"type":"sell",
"amount":1.00000000,
"rate":3.00000000,
"timestamp_created":1342448420,
"status":0
}
"343157":{//get this object name
"pair":"usd_eur",
"type":"sell",
"amount":1.00000000,
"rate":3.00000000,
"timestamp_created":1342448420,
"status":0
}
}
}
您应该在版本>= 5.X中使用QtCore中的json支持。您可以使用以下代码执行此操作:
杰森.txt
{
"success": 1,
"return": {
"343152": {
"pair": "usd_eur",
"type": "sell",
"amount": 1.00000000,
"rate": 3.00000000,
"timestamp_created": 1342448420,
"status": 0
},
"343157": {
"pair": "usd_eur",
"type": "sell",
"amount ":1.00000000,
"rate": 3.00000000,
"timestamp_created": 1342448420,
"status": 0
}
}
}
主.cpp
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QByteArray>
#include <QStringList>
#include <QDebug>
#include <QFile>
int main()
{
QFile file("json.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "File open error:" << file.errorString();
return 1;
}
QByteArray jsonByteArray = file.readAll();
file.close();
QJsonParseError jsonParseError;
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonByteArray, &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError) {
qDebug() << "Error happened:" << jsonParseError.errorString();
return 1;
}
if (!jsonDocument.isObject()) {
qDebug() << "The json data is not an object";
return 1;
}
QJsonObject mainJsonObject(jsonDocument.object());
QJsonValue returnJsonValue = mainJsonObject.value(QStringLiteral("return"));
if (!returnJsonValue.isObject()) {
qDebug() << "The json data is not an object";
return 1;
}
qDebug() << "KEYS WANTED:" << returnJsonValue.toObject().keys();
return 0;
}
输出
g++ -Wall -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -I/usr/include -lQt5Core main.cpp && ./a.out
KEYS WANTED: ("343152", "343157")
由于您的帖子被标记为 qjson,我假设您正在使用它......
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (json, &ok).toMap(); // json is a QByteArray w. the data
QVariantMap returnMap = result["return"].toMap();
// iterate your map to get the names you're interested in.
for(QVariantMap::const_iterator iter = returnMap.begin(); iter != returnMap.end(); ++iter) {
qDebug() << iter.key();
}
// Do whatever you need with yourObj..
见 http://qjson.sourceforge.net/usage/
相关文章:
- 如何在C++中获取该对象的类声明中对象的地址?
- 无法获取webot::PositionSensor对象中位置传感器的值
- 最多获取2个对象
- 从 Boost ASIO 获取 epoll 描述符 io_service对象
- 如何在新的派生对象中获取基本对象的数据?
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 数组中不同类型的对象并获取每个对象的类型
- push_back通过自行创建的对象获取最后一个元素的向量
- 我可以从列表中获取对象并复制它们,但如何删除我复制的对象?
- 在C++中从内部类对象获取外部类对象
- 如何从 GDI+ 图元文件对象获取原始图元文件字节
- C++ std::队列推送弹出两个不同的对象获取第一个对象
- 尝试从另一个类对象获取值时出现分段错误
- 从元对象获取属性更改通知程序
- 从 Json 对象获取非缩进字符串
- 从套接字对象获取fd或句柄
- (如何)我可以从IWbemClassObject对象获取GUID吗
- 使用不同类中的类对象获取不正确的数据
- OpenCV Mat 对象 - 获取数据长度
- 在 c++ 中,如何从对象获取到静态成员