C++ 如何使用nlohmann lib解析json?
我使用这个库进行json解析 我需要解析这个json文件C++ 如何使用nlohmann lib解析json?,c++,json,nlohmann-json,C++,Json,Nlohmann Json,我使用这个库进行json解析 我需要解析这个json文件 { "emulators": [ { "general_info": { "dev_id": "0123456789", "model": "my_model", "model_full": "my_full_model", "serial_num": "my_serial_num" } } ] } 这就是我试图做到的 voi
{
"emulators": [
{
"general_info": {
"dev_id": "0123456789",
"model": "my_model",
"model_full": "my_full_model",
"serial_num": "my_serial_num"
}
}
]
}
这就是我试图做到的
void EmulatorCameraConfigParser::GetEmulatorContextObjFrom(std::string path_to_configuration_json, std::vector<emulator_context::EmulatorContextObj> * out_arr)
{
json jf = Utils::get_json_data(path_to_configuration_json);
if (jf == nullptr)
{
//TODO print - "Configuration json file for TV_CamFromFiles is null";
}
else
{
try
{
for (auto& elem : jf[EMULATORS])
{
emulator_context::EmulatorContextObj tmp;
tmp.m_general_info = &get_general_info(elem[GENERAL_INFO]);
out_arr->push_back(tmp);
}
}
catch (std::exception& ex)
{
//TODO print error
std::string errMsg = "Exeption during parsing configuration json file for TV_CamFromFiles :: ";
errMsg += ex.what();
}
}
}
void EmulatorCameraConfigParser::GetEmulatorContextObjFrom(std::string path_to_configuration_json,std::vector*out_arr)
{
json jf=Utils::获取\u json\u数据(路径到\u配置\u json);
如果(jf==nullptr)
{
//TODO print—“TV_CamFromFiles的配置json文件为空”;
}
其他的
{
尝试
{
对于(auto&elem:jf[模拟器])
{
emulator_context::EmulatorContextExtrabj tmp;
tmp.m_general_info=&get_general_info(elem[general_info]);
出站->后推(tmp);
}
}
捕获(标准::异常和例外)
{
//TODO打印错误
std::string errMsg=“解析TV_CamFromFiles:的配置json文件时的Exeption::”;
errMsg+=ex.what();
}
}
}
emulator\u context::GeneralInfo-EmulatorCameraConfigParser::get\u-general\u-info(const-json&json)
{
emulator_context::GeneralInfo GeneralInfo;
尝试
{
if(json!=nullptr)
{
generalInfo.m_dev_id=json.at(dev_id).get();
generalInfo.m_serial_num=json.at(serial_num.get();
}
}
捕获(标准::异常和例外)
{
//TODO打印此错误
std::string errMsg=“解析TV_CamFromFiles:的配置json文件时的Exeption::”;
errMsg+=ex.what();
}
返回generalInfo;
}
但结果我什么也得不到,所以我几乎可以肯定问题就在这里
tmp.m_general_info=&get_general_info(elem[general_info])代码>
我认为为了获得blockgeneral\u info
我需要使用其他方法,但哪种方法呢?从文件加载json
比这更简单
#include <nlohmann/json.hpp>
#include <iostream>
#include <sstream>
#include <string>
using json = nlohmann::json;
int main() {
// open the json file - here replaced with a std::istringstream containing the json data
std::istringstream file(R"json({
"emulators": [
{
"general_info": {
"dev_id": "0123456789",
"model": "my_model",
"model_full": "my_full_model",
"serial_num": "my_serial_num"
}
}
]
})json");
// declare your json object and stream from the file
json j;
file >> j;
// the file is now fully parsed
// access fields
for(json& o : j["emulators"]) {
json& gi = o["general_info"];
std::cout << gi["dev_id"] << '\n';
std::cout << gi["model"] << '\n';
std::cout << gi["model_full"] << '\n';
std::cout << gi["serial_num"] << '\n';
}
}
您还可以添加自己的序列化程序支持:
#include <nlohmann/json.hpp>
#include <iostream>
#include <sstream>
#include <string>
using json = nlohmann::json;
struct general_info {
std::string dev_id;
std::string model;
std::string model_full;
std::string serial_num;
};
// conversion functions for your general_info
void to_json(json& j, const general_info& gi) {
j = json{{"dev_id", gi.dev_id},
{"model", gi.model},
{"model_full", gi.model_full},
{"serial_num", gi.serial_num}};
}
void from_json(const json& j, general_info& gi) {
j.at("dev_id").get_to(gi.dev_id);
j.at("model").get_to(gi.model);
j.at("model_full").get_to(gi.model_full);
j.at("serial_num").get_to(gi.serial_num);
}
int main() {
std::istringstream file(R"json({
"emulators": [
{
"general_info": {
"dev_id": "0123456789",
"model": "my_model",
"model_full": "my_full_model",
"serial_num": "my_serial_num"
}
}
]
})json");
// declare your json object and stream from the file
json j;
file >> j;
// convert a json array of "general_info" to a std::vector<general_info>
json& arr = j["emulators"];
std::vector<general_info> gis;
std::for_each(arr.begin(), arr.end(), [&gis](const json& o) {
if(auto it = o.find("general_info"); it != o.end()) {
gis.push_back(it->get<general_info>());
}
});
for(general_info& gi : gis) {
std::cout << gi.dev_id << '\n';
std::cout << gi.model << '\n';
std::cout << gi.model_full << '\n';
std::cout << gi.serial_num << '\n';
}
}
#包括
#包括
#包括
#包括
使用json=nlohmann::json;
结构通用信息{
std::字符串dev_id;
std::字符串模型;
std::字符串模型_full;
std::字符串序列号;
};
//通用信息的转换函数
void to_json(json&j、const general_info&gi){
j=json{{“dev_id”,gi.dev_id},
{“model”,gi.model},
{“model_full”,gi.model_full},
{“serial_num”,gi.serial_num};
}
来自_json的void(const json&j、general_info&gi){
j、 at(“dev_id”).get_to(gi.dev_id);
j、 在(“模型”)。到达(gi.model);
j、 在(“型号满”)。到达(gi.型号满);
j、 在(“序列号”)。获取到(gi.序列号);
}
int main(){
std::istringstream文件(R“json({
“仿真器”:[
{
“一般信息”:{
“开发id”:“0123456789”,
“模型”:“我的模型”,
“型号满”:“我的型号满”,
“序列号”:“我的序列号”
}
}
]
})json”);
//从文件中声明json对象和流
j;
文件>>j;
//将“general_info”的json数组转换为std::vector
json&arr=j[“模拟器”];
矢量gis;
std::for_each(arr.begin()、arr.end()、[&gis](const json&o){
if(auto it=o.find(“general_info”);it!=o.end(){
gis.push_back(it->get());
}
});
适用于(通用信息和地理信息系统:地理信息系统){
std::cout从文件加载json
比这个简单
#include <nlohmann/json.hpp>
#include <iostream>
#include <sstream>
#include <string>
using json = nlohmann::json;
int main() {
// open the json file - here replaced with a std::istringstream containing the json data
std::istringstream file(R"json({
"emulators": [
{
"general_info": {
"dev_id": "0123456789",
"model": "my_model",
"model_full": "my_full_model",
"serial_num": "my_serial_num"
}
}
]
})json");
// declare your json object and stream from the file
json j;
file >> j;
// the file is now fully parsed
// access fields
for(json& o : j["emulators"]) {
json& gi = o["general_info"];
std::cout << gi["dev_id"] << '\n';
std::cout << gi["model"] << '\n';
std::cout << gi["model_full"] << '\n';
std::cout << gi["serial_num"] << '\n';
}
}
您还可以添加自己的序列化程序支持:
#include <nlohmann/json.hpp>
#include <iostream>
#include <sstream>
#include <string>
using json = nlohmann::json;
struct general_info {
std::string dev_id;
std::string model;
std::string model_full;
std::string serial_num;
};
// conversion functions for your general_info
void to_json(json& j, const general_info& gi) {
j = json{{"dev_id", gi.dev_id},
{"model", gi.model},
{"model_full", gi.model_full},
{"serial_num", gi.serial_num}};
}
void from_json(const json& j, general_info& gi) {
j.at("dev_id").get_to(gi.dev_id);
j.at("model").get_to(gi.model);
j.at("model_full").get_to(gi.model_full);
j.at("serial_num").get_to(gi.serial_num);
}
int main() {
std::istringstream file(R"json({
"emulators": [
{
"general_info": {
"dev_id": "0123456789",
"model": "my_model",
"model_full": "my_full_model",
"serial_num": "my_serial_num"
}
}
]
})json");
// declare your json object and stream from the file
json j;
file >> j;
// convert a json array of "general_info" to a std::vector<general_info>
json& arr = j["emulators"];
std::vector<general_info> gis;
std::for_each(arr.begin(), arr.end(), [&gis](const json& o) {
if(auto it = o.find("general_info"); it != o.end()) {
gis.push_back(it->get<general_info>());
}
});
for(general_info& gi : gis) {
std::cout << gi.dev_id << '\n';
std::cout << gi.model << '\n';
std::cout << gi.model_full << '\n';
std::cout << gi.serial_num << '\n';
}
}
#包括
#包括
#包括
#包括
使用json=nlohmann::json;
结构通用信息{
std::字符串dev_id;
std::字符串模型;
std::字符串模型_full;
std::字符串序列号;
};
//通用信息的转换函数
void to_json(json&j、const general_info&gi){
j=json{{“dev_id”,gi.dev_id},
{“model”,gi.model},
{“model_full”,gi.model_full},
{“serial_num”,gi.serial_num};
}
来自_json的void(const json&j、general_info&gi){
j、 at(“dev_id”).get_to(gi.dev_id);
j、 在(“模型”)。到达(gi.model);
j、 在(“型号满”)。到达(gi.型号满);
j、 在(“序列号”)。获取到(gi.序列号);
}
int main(){
std::istringstream文件(R“json({
“仿真器”:[
{
“一般信息”:{
“开发id”:“0123456789”,
“模型”:“我的模型”,
“型号满”:“我的型号满”,
“序列号”:“我的序列号”
}
}
]
})json”);
//从文件中声明json对象和流
j;
文件>>j;
//将“general_info”的json数组转换为std::vector
json&arr=j[“模拟器”];
矢量gis;
std::for_each(arr.begin()、arr.end()、[&gis](const json&o){
if(auto it=o.find(“general_info”);it!=o.end(){
gis.push_back(it->get());
}
});
适用于(通用信息和地理信息系统:地理信息系统){
std::couttmp.m\u general\u info=&get\u general\u info(elem[general\u info])
您正在存储一个临时文件的地址。您的编译器肯定对此有意见吗?请提供一个。什么是Utils::get_json_data
?例如,您可能还可以省略try-catch内容。相反,提供一个生成所述问题的最小main
方法。tmp.m_general\u info=&get_general_信息(元素[一般信息])
您正在存储一个临时文件的地址。您的编译器肯定对此有意见吗?请提供一个。什么是Utils::get_json_data
?例如,您可能还可以省略try-catch内容。相反,提供一个生成所述问题的最小main
-方法。但是我想知道如何避免JUt打印json,但如何访问每个确切的field@AlekseyTimoshchenko我再次更新了答案,添加了序列化程序支持。但我想知道如何不仅打印json,还想知道如何访问每个确切的field@AlekseyTimoshchenko我再次更新了答案,添加了序列化程序支持。