json.hpp库放在文章末尾
1、遍历json文件读写
(1) Szenario 1 des Einfügens neuer Schlüssel-Wert-Paare in JSON.
Die ursprüngliche JSON-Datei sieht wie folgt aus:
{
"Connection": {
"IpAddress": "192.168.20.1",
"Rock": 0,
"Solt": 1
},
"DataBaseNumber":7,
"hardWare":{
"Axis1_offset_Enable": "0.0",
"Axis1_offset_Stop": "0.1",
"Axis1_offset_Speed": "2.0",
"Axis1_offset_Mov": "4.0",
"Axis1_offset_Pos": "6.0",
"Axis1_offset_Busy": "8.1",
"Axis2_offset_Enable": "10.0",
"Axis2_offset_Stop": "10.1",
"Axis2_offset_Speed": "12.0",
"Axis2_offset_Mov": "14.0",
"Axis2_offset_Pos": "16.0",
"Axis2_offset_Busy": "18.1"
}
}
Nun möchte ich am Ende ein neues Schlüssel-Wert-Paar wie folgt einfügen:
{
"Connection": {
"IpAddress": "192.168.20.1",
"Rock": 0,
"Solt": 1
},
"DataBaseNumber":7,
"hardWare":{
"Axis1_offset_Enable": "0.0",
"Axis1_offset_Stop": "0.1",
"Axis1_offset_Speed": "2.0",
"Axis1_offset_Mov": "4.0",
"Axis1_offset_Pos": "6.0",
"Axis1_offset_Busy": "8.1",
"Axis2_offset_Enable": "10.0",
"Axis2_offset_Stop": "10.1",
"Axis2_offset_Speed": "12.0",
"Axis2_offset_Mov": "14.0",
"Axis2_offset_Pos": "16.0",
"Axis2_offset_Busy": "18.1"
},
"time":{
"maxtime":100,
"mintime":0
}
}
Code:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
std::ifstream file("test.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
json jsonData;
file >> jsonData;
file.close();
// 创建新的键值对
json timeData = {
{
"maxtime", 100},
{
"mintime", 0}
};
// 将新的键值对插入到现有的 JSON 数据中
jsonData["time"] = timeData;
// 将更新后的 JSON 数据写入文件
std::ofstream outputFile("test.json");
if (!outputFile.is_open()) {
std::cerr << "Failed to open file for writing." << std::endl;
return 1;
}
outputFile << std::setw(4) << jsonData << std::endl; // 美化输出,每个元素缩进四个空格
outputFile.close();
return 0;
}
(2) Fügen Sie neue Schlüssel-Wert-Paare in JSON ein. Szenario 2.
Die ursprüngliche JSON-Datei lautet wie folgt:
{
"Connection": {
"IpAddress": "192.168.20.1",
"Rock": 0,
"Solt": 1
},
"DataBaseNumber":7,
"hardWare":{
"Axis1_offset_Enable": "0.0",
"Axis1_offset_Stop": "0.1",
"Axis1_offset_Speed": "2.0",
"Axis1_offset_Mov": "4.0",
"Axis1_offset_Pos": "6.0",
"Axis1_offset_Busy": "8.1",
"Axis2_offset_Enable": "10.0",
"Axis2_offset_Stop": "10.1",
"Axis2_offset_Speed": "12.0",
"Axis2_offset_Mov": "14.0",
"Axis2_offset_Pos": "16.0",
"Axis2_offset_Busy": "18.1"
}
}
Jetzt möchte ich am Ende ein Array einfügen, wie unten gezeigt:
{
"Connection": {
"IpAddress": "192.168.20.1",
"Rock": 0,
"Solt": 1
},
"DataBaseNumber":7,
"hardWare":{
"Axis1_offset_Enable": "0.0",
"Axis1_offset_Stop": "0.1",
"Axis1_offset_Speed": "2.0",
"Axis1_offset_Mov": "4.0",
"Axis1_offset_Pos": "6.0",
"Axis1_offset_Busy": "8.1",
"Axis2_offset_Enable": "10.0",
"Axis2_offset_Stop": "10.1",
"Axis2_offset_Speed": "12.0",
"Axis2_offset_Mov": "14.0",
"Axis2_offset_Pos": "16.0",
"Axis2_offset_Busy": "18.1"
},
"ROIS":[
{
"Name":"ROI1",
"weight":1
},
{
"Name":"ROI2",
"weight":0.5
}
]
}
Code:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
std::ifstream file("your_file.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
json jsonData;
file >> jsonData;
file.close();
// 创建新的 ROIS 数据
json roisData = {
{
"Name", "ROI1"},
{
"weight", 1}
};
json roisData2 = {
{
"Name", "ROI2"},
{
"weight", 0.5}
};
// 将新的 ROIS 数据插入到 JSON 数据末尾
jsonData["ROIS"].push_back(roisData);
jsonData["ROIS"].push_back(roisData2);
// 将更新后的 JSON 数据写入文件
std::ofstream outputFile("updated_file.json");
if (!outputFile.is_open()) {
std::cerr << "Failed to open file for writing." << std::endl;
return 1;
}
outputFile << std::setw(4) << jsonData << std::endl; // 美化输出,每个元素缩进四个空格
outputFile.close();
return 0;
}
(3) Fügen Sie neue Schlüssel-Wert-Paare in JSON ein. Szenario 3.
Die ursprüngliche JSON-Datei lautet wie folgt:
{
"test": [
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1
}
]
},
"Index": 1
},
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1
}
]
},
"Index": 2
}
]
}
Jetzt möchte ich ein neues Schlüssel-Wert-Paar unter Index und Gewicht in die beiden Inhalte des Arrays einfügen, wie unten gezeigt:
{
"test": [
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1,
"width": 100
}
]
},
"Index": 1,
"name": "vimba"
},
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1,
"width": 100
}
]
},
"Index": 2,
"name": "vimba"
}
]
}
Code:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
std::ifstream file("your_file.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
json jsonData;
file >> jsonData;
file.close();
// 遍历 AutoExposure 数组
if (jsonData.find("test") != jsonData.end() && jsonData["test"].is_array()) {
for (auto& entry : jsonData["test"]) {
// 获取 ROIS 数组
if (entry.find("IsUseROIs") != entry.end() && entry["IsUseROIs"].find("ROIS") != entry["IsUseROIs"].end()) {
// 在每个 ROIS 数组元素后插入新键值对 "width": 100
for (auto& rois_entry : entry["IsUseROIs"]["ROIS"]) {
rois_entry["width"] = 100;
}
}
// 在每个对象中添加新键值对 "name": "vimba"
entry["name"] = "vimba";
}
}
// 将更新后的 JSON 数据写入文件
std::ofstream outputFile("updated_file.json");
if (!outputFile.is_open()) {
std::cerr << "Failed to open file for writing." << std::endl;
return 1;
}
outputFile << std::setw(4) << jsonData << std::endl; // 美化输出,每个元素缩进四个空格
outputFile.close();
return 0;
}
(4) Ändern Sie die ursprüngliche Schlüssel-Wert-Paar-Situation 1.
Die ursprüngliche JSON-Datei lautet wie folgt:
{
"test": [
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1
}
]
},
"Index": 1
},
{
"FixedParameters": {
"bit_depth": 255,
"dynamic_range": 0.8
},
"IsUseROIs":{
"isUseROIs":false,
"ROIS":[
{
"Name":"ROI1",
"weight":1
}
]
},
"Index": 2
}
]
}
Jetzt möchte ich die Index- und Gewichtswerte in jedem Array-Element ändern. Der Code lautet wie folgt:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
std::ifstream file("your_file.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
json jsonData;
file >> jsonData;
file.close();
// 遍历 AutoExposure 数组
if (jsonData.find("test") != jsonData.end() && jsonData["test"].is_array()) {
for (auto& entry : jsonData["test"]) {
// 修改每个对象的 "Index" 值为 10 和 20
entry["Index"] = (entry["Index"] == 1) ? 10 : 20; //这里可以通过json其他键值对判断目前遍历的是数组第几个元素
// 修改每个对象的 "weight" 值为 0.5 和 0.7
if (entry.find("IsUseROIs") != entry.end() && entry["IsUseROIs"].find("ROIS") != entry["IsUseROIs"].end()) {
for (auto& rois_entry : entry["IsUseROIs"]["ROIS"]) {
rois_entry["weight"] = (rois_entry["weight"] == 1) ? 0.5 : 0.7; //这里可以通过json其他键值对判断目前遍历的是数组第几个元素
}
}
}
}
// 将更新后的 JSON 数据写入文件
std::ofstream outputFile("updated_file.json");
if (!outputFile.is_open()) {
std::cerr << "Failed to open file for writing." << std::endl;
return 1;
}
outputFile << std::setw(4) << jsonData << std::endl; // 美化输出,每个元素缩进四个空格
outputFile.close();
return 0;
}
(5) Ändern Sie das ursprüngliche Schlüssel-Wert-Paar in Fall 2.
Die ursprüngliche JSON-Datei lautet wie folgt:
{
"Connection": {
"IpAddress": "192.168.20.1",
"Rock": 0,
"Solt": 1
},
"DataBaseNumber":7,
"hardWare":{
"Axis1_offset_Enable": "0.0",
"Axis1_offset_Stop": "0.1",
"Axis1_offset_Speed": "2.0",
"Axis1_offset_Mov": "4.0",
"Axis1_offset_Pos": "6.0",
"Axis1_offset_Busy": "8.1",
"Axis2_offset_Enable": "10.0",
"Axis2_offset_Stop": "10.1",
"Axis2_offset_Speed": "12.0",
"Axis2_offset_Mov": "14.0",
"Axis2_offset_Pos": "16.0",
"Axis2_offset_Busy": "18.1"
}
}
Jetzt möchte ich Axis2_offset_Busy auf 18.2 ändern. Der Code lautet wie folgt:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
std::ifstream file("your_file.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
json jsonData;
file >> jsonData;
file.close();
// 修改特定键的值
if (jsonData.find("hardWare") != jsonData.end()) {
jsonData["hardWare"]["Axis2_offset_Busy"] = "18.2";
}
// 将更新后的 JSON 数据写入文件
std::ofstream outputFile("updated_file.json");
if (!outputFile.is_open()) {
std::cerr << "Failed to open file for writing." << std::endl;
return 1;
}
outputFile << std::setw(4) << jsonData << std::endl; // 美化输出,每个元素缩进四个空格
outputFile.close();
return 0;
}