json文件读写

写入:

#include <jsoncpp/json/json.h>

void main(int argc, char **argv) {
    Json::Value root;
    root["ref_cam_id"] = 0;

    Json::Value cam0;
    cam0["camera_id"] = 0;
    cam0["width"] = 1920;
    cam0["height"] = 1080;
    cam0["intrinsics"][0] = 1000;
    cam0["intrinsics"][1] = 1000;
    cam0["intrinsics"][2] = 960;
    cam0["intrinsics"][3] = 540;

    Json::Value cam1;
    cam1["camera_id"] = 1;
    cam1["width"] = 1280;
    cam1["height"] = 720;
    cam1["intrinsics"][0] = 900;
    cam1["intrinsics"][1] = 900;
    cam1["intrinsics"][2] = 640;
    cam1["intrinsics"][3] = 360;

    root["cameras"][0] = cam0;
    //root["cameras"][1] = cam1;
    root["cameras"].append(cam1);

    Json::StyledWriter writer;
    std::ofstream out_file;
    out_file.open("/home/alan/Desktop/test.json");
    //out_file << writer.write(root);
    out_file << root.toStyledString();
    out_file.close();
}

key 默认按照字典排序

读取:

#include <jsoncpp/json/json.h>

void main() {
    std::string camera_config_path = "/home/alan/Desktop/test.json";
    std::ifstream file(camera_config_path);
    if(!file.is_open()) {
        std::cout << "Cannot open file " << camera_config_path << std::endl;
    }

    //Json::Reader reader;
    //Json::Value root;
    //if (!reader.parse(file, root))
    //{
    //    return -1;
    //}

    Json::Value root;
    Json::CharReaderBuilder builder;
    JSONCPP_STRING errs;
    bool ok = parseFromStream(builder, file, &root, &errs);
    if(!ok) {
        std::cout << "Error while parsing json stream: " << errs << std::endl;
    }

    Json::Value cam0_node = root["cameras"][0];
    int width = cam0_node["width"].asInt();
    int height = cam0_node["height"].asInt();
    Json::Value intrinsics_node = cam0_node["intrinsics"];
    if (intrinsics_node.size() != 4) {
        std::cout << "error" << std::endl;
    }
    double fx = intrinsics_node[0].asDouble();
    double fy = intrinsics_node[1].asDouble();
    double cx = intrinsics_node[2].asDouble();
    double cy = intrinsics_node[3].asDouble();

    Json::Value cam1_node = root["cameras"][1];
    // ...
    std::cout << width << " " << height << " " << fx << " " << fy << " " << cx << " " << cy << std::endl;
}

CMakeLists.txt:

find_package(jsoncpp REQUIRED)
include_directories(${JSONCPP_INCLUDE_DIR})

add_executable(main src/main.cpp)
target_link_libraries(main jsoncpp)

json与jsonlines转换:

sudo apt-get install jq
cat test.txt | jq '.' > test.json # json->jsonlines
cat test.json | jq -c '.' > test.txt # jsonlines->json

参考:

JSON Lines

JSON, JSONlines, and jq as a better grep


JsonCpp如何判断是否有某个KEY,使用json[“key”]和isXXX的函数即可。
如果json中没有key键,则会创建一个空成员或者返回一个空成员。

bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isUInt() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C JSON文件读写是指使用C语言编程实现对JSON格式文件的读取和写入操作。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于Web应用程序中传输和存储数据。 在C语言中,我们可以使用第三方库,如cJSON库来进行JSON文件的读写操作。首先,我们需要通过该库的函数将JSON文件加载到内存中,可以使用cJSON_ParseFile函数来实现这一步骤。该函数会返回一个cJSON对象,表示JSON文件中的数据结构。 读取JSON文件后,我们可以使用cJSON_GetObjectItem函数来获取JSON对象中的具体字段值。该函数接受两个参数,第一个参数是表示JSON对象的cJSON对象,第二个参数是字段名,返回该字段对应的值。通过不断调用该函数,我们便可以获取到JSON文件中的所有字段值。 在对JSON文件进行写入操作时,我们需要先创建一个空的cJSON对象,使用cJSON_CreateObject函数来创建。然后,使用cJSON_AddItemToObject函数,将字段和值逐个添加到cJSON对象中。最后,使用cJSON_Print函数将cJSON对象转换为字符串,并将字符串写入到文件中。 总结一下,C JSON文件的读写操作需要使用第三方库cJSON,通过函数cJSON_ParseFile读取JSON文件、使用cJSON_GetObjectItem获取字段值、创建cJSON对象、使用cJSON_AddItemToObject添加字段和值、使用cJSON_Print将cJSON对象转换为字符串,并使用文件操作函数将字符串写入到文件中。这样就实现了C语言中对JSON文件的读写操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值