请把我要求的代码写出来
时间: 2024-03-08 15:48:14 浏览: 27
好的,下面是一个示例代码,演示如何在Qt中将从server端接收到的JSON格式的字符串转换为JSON对象:
```C++
// 假设strJson是从server端接收到的JSON格式的字符串
QString strJson = "{\"map_info\":{\"map_id\":1,\"map_name\":\"map_name_1\",\"origin0\":1.1,\"origin1\":2,\"resolution\":1111,\"free_thresh\":1.1,\"occupied_thresh\":1.1,\"height\":3,\"width\":4,\"case_number\":5,\"case_info\":[{\"case_id\":1,\"point_num\":1,\"wall_num\":1,\"route_num\":1,\"point_info\":[{\"point_id\":1,\"point_type\":1,\"point_x\":1.0,\"point_y\":1.0,\"yaw_z\":1.0,\"case_id\":1},{\"point_id\":2,\"point_type\":2,\"point_x\":2.0,\"point_y\":2.0,\"yaw_z\":2.0,\"case_id\":1}],\"wall_info\":[{\"wall_id\":1,\"x1\":1.0,\"y1\":1.0,\"x2\":1.0,\"y2\":1.0,\"case_id\":1},{\"wall_id\":1,\"x1\":1.0,\"y1\":1.0,\"x2\":1.0,\"y2\":1.0,\"case_id\":1}],\"route_info\":[{\"route_id\":1,\"number\":1,\"case_id\":1,\"route_point_list\":[{\"type\":1,\"x\":1.0,\"y\":1.0,\"z\":1.0},{\"type\":1,\"x\":1.0,\"y\":1.0,\"z\":1.0}]},{\"route_id\":2,\"number\":1,\"case_id\":1,\"route_point_list\":[{\"type\":1,\"x\":1.0,\"y\":1.0,\"z\":1.0},{\"type\":1,\"x\":1.0,\"y\":1.0,\"z\":1.0}]}]}]}}";
// 将JSON格式的字符串转换为JSON对象
QJsonDocument jsonDoc = QJsonDocument::fromJson(strJson.toUtf8());
QJsonObject jsonObj = jsonDoc.object();
// 解析JSON对象
int mapId = jsonObj["map_info"].toObject()["map_id"].toInt();
QString mapName = jsonObj["map_info"].toObject()["map_name"].toString();
double origin0 = jsonObj["map_info"].toObject()["origin0"].toDouble();
double origin1 = jsonObj["map_info"].toObject()["origin1"].toDouble();
int resolution = jsonObj["map_info"].toObject()["resolution"].toInt();
double freeThresh = jsonObj["map_info"].toObject()["free_thresh"].toDouble();
double occupiedThresh = jsonObj["map_info"].toObject()["occupied_thresh"].toDouble();
int height = jsonObj["map_info"].toObject()["height"].toInt();
int width = jsonObj["map_info"].toObject()["width"].toInt();
int caseNumber = jsonObj["map_info"].toObject()["case_number"].toInt();
QJsonArray caseInfo = jsonObj["map_info"].toObject()["case_info"].toArray();
// 输出解析结果
qDebug() << "map_id:" << mapId;
qDebug() << "map_name:" << mapName;
qDebug() << "origin0:" << origin0;
qDebug() << "origin1:" << origin1;
qDebug() << "resolution:" << resolution;
qDebug() << "free_thresh:" << freeThresh;
qDebug() << "occupied_thresh:" << occupiedThresh;
qDebug() << "height:" << height;
qDebug() << "width:" << width;
qDebug() << "case_number:" << caseNumber;
qDebug() << "case_info:";
for (int i = 0; i < caseInfo.size(); i++) {
QJsonObject caseObj = caseInfo[i].toObject();
int caseId = caseObj["case_id"].toInt();
int pointNum = caseObj["point_num"].toInt();
int wallNum = caseObj["wall_num"].toInt();
int routeNum = caseObj["route_num"].toInt();
QJsonArray pointInfo = caseObj["point_info"].toArray();
QJsonArray wallInfo = caseObj["wall_info"].toArray();
QJsonArray routeInfo = caseObj["route_info"].toArray();
qDebug() << "case_id:" << caseId;
qDebug() << "point_num:" << pointNum;
qDebug() << "wall_num:" << wallNum;
qDebug() << "route_num:" << routeNum;
qDebug() << "point_info:";
for (int j = 0; j < pointInfo.size(); j++) {
QJsonObject pointObj = pointInfo[j].toObject();
int pointId = pointObj["point_id"].toInt();
int pointType = pointObj["point_type"].toInt();
double pointX = pointObj["point_x"].toDouble();
double pointY = pointObj["point_y"].toDouble();
double yawZ = pointObj["yaw_z"].toDouble();
int caseId = pointObj["case_id"].toInt();
qDebug() << " point_id:" << pointId;
qDebug() << " point_type:" << pointType;
qDebug() << " point_x:" << pointX;
qDebug() << " point_y:" << pointY;
qDebug() << " yaw_z:" << yawZ;
qDebug() << " case_id:" << caseId;
}
qDebug() << "wall_info:";
for (int j = 0; j < wallInfo.size(); j++) {
QJsonObject wallObj = wallInfo[j].toObject();
int wallId = wallObj["wall_id"].toInt();
double x1 = wallObj["x1"].toDouble();
double y1 = wallObj["y1"].toDouble();
double x2 = wallObj["x2"].toDouble();
double y2 = wallObj["y2"].toDouble();
int caseId = wallObj["case_id"].toInt();
qDebug() << " wall_id:" << wallId;
qDebug() << " x1:" << x1;
qDebug() << " y1:" << y1;
qDebug() << " x2:" << x2;
qDebug() << " y2:" << y2;
qDebug() << " case_id:" << caseId;
}
qDebug() << "route_info:";
for (int j = 0; j < routeInfo.size(); j++) {
QJsonObject routeObj = routeInfo[j].toObject();
int routeId = routeObj["route_id"].toInt();
int number = routeObj["number"].toInt();
int caseId = routeObj["case_id"].toInt();
QJsonArray routePointList = routeObj["route_point_list"].toArray();
qDebug() << " route_id:" << routeId;
qDebug() << " number:" << number;
qDebug() << " case_id:" << caseId;
qDebug() << " route_point_list:";
for (int k = 0; k < routePointList.size(); k++) {
QJsonObject pointObj = routePointList[k].toObject();
int type = pointObj["type"].toInt();
double x = pointObj["x"].toDouble();
double y = pointObj["y"].toDouble();
double z = pointObj["z"].toDouble();
qDebug() << " type:" << type;
qDebug() << " x:" << x;
qDebug() << " y:" << y;
qDebug() << " z:" << z;
}
}
}
```
在上面的代码中,我们首先使用QJsonDocument类将JSON格式的字符串转换为JSON对象,然后使用QJsonObject、QJsonArray等类获取JSON对象中的各个字段和数组元素,并输出解析结果。需要注意的是,由于JSON格式的字符串中包含多层的JSON对象和JSON数组,因此需要使用嵌套的方式进行解析。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)