• c++ | json库的使用 | josn静态库生成


    环境centos

    github上下载json相关包
    然后将文件移入指定路径(看个人)
    需要安装cmake
    在该路径下创建如下路径 /build/release
    然后在 /release跑cmake 编译整个项目(下载过来的文件)
    最后再 make 一遍
    找到静态库 我的静态库相对路径为 /build/release/src/lib_json
    libjsoncpp.a
    还需要配置头文件路径,导入json头文件
    注意:不要下太新的json包,因为很多地方都会涉及版本不一致问题,需要安装更新版本的gcc、cmake、我用的是较低版本
    较低的版本也容易适配

    我安装的是0.10.7

    cmake \
    -D CMAKE_BUILD_TYPE=release\
    -D BUILD_STATIC_LIBS=ON \
    -D BUILD_SHARED_LIBS=OFF \
    -D CMAKE_INSTALL_PREFIX=/usr/local/jsoncpp \
    -D CMAKE_C_COMPILER=/usr/bin/gcc \
    -D CMAKE_CXX_COMPILER=/usr/bin/g++ \
    -D CMAKE_CXX_FLAGS="-fPIC" \
    -D CMAKE_C_FLAGS="-fPIC" \
    -G "Unix Makefiles" ../..
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    之前下了最新的,但是有一些问题,报错

    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /home/drcomtudong/jsoncpp/lib/libjsoncpp.a(json_reader.cpp.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /home/drcomtudong/jsoncpp/lib/libjsoncpp.a(json_value.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /home/drcomtudong/jsoncpp/lib/libjsoncpp.a(json_writer.cpp.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: final link failed: Nonrepresentable section on output
    collect2: error: ld returned 1 exit status
    make: *** [HttpAuth.so] Error 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    先麦格雷,到后面有时间再来分析一下。

    //json ->c++ string/int
    #include 
    #include  // 包含JsonCpp头文件
    
    int main() {
        std::string jsonText = "{\"name\": \"John\", \"age\": 30}";
    
        Json::Value root;
        Json::Reader reader;
    
        if (reader.parse(jsonText, root)) {
            // 成功解析JSON数据
            std::string name = root["name"].asString();
            int age = root["age"].asInt();
    
            std::cout << "Name: " << name << std::endl;
            std::cout << "Age: " << age << std::endl;
        } else {
            // 解析失败
            std::cout << "Failed to parse JSON" << std::endl;
        }
    
        return 0;
    }
    
    //c++ ->json	
    
    #include 
    #include 
    
    struct Person {
        std::string name;
        int age;
    };
    
    int main() {
        // 创建一个C++结构实例
        Person person;
        person.name = "John";
        person.age = 30;
    
        // 创建一个Json::Value对象,用于存储转换后的JSON数据
        Json::Value jsonPerson;
    
        // 将C++数据转换为JSON数据
        jsonPerson["name"] = person.name;
        jsonPerson["age"] = person.age;
    
        // 使用Json::StreamWriterBuilder将JSON数据序列化为字符串
        Json::StreamWriterBuilder writer;
        std::string jsonString = Json::writeString(writer, jsonPerson);
    
        // 打印生成的JSON字符串
        std::cout << "JSON String: " << jsonString << std::endl;
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    总结一下,没想到cmake还能这么用,纯纯骚操作!

  • 相关阅读:
    TFTP协议解析及C/C++代码实现
    Keepalived+HAProxy基于ACL实现单IP多域名负载功能
    众所周知,配置即代码≠基础设置即代码
    MySQL 慢查询
    leetcode 刷题 log day 45
    简易绘图 DataFrame.plot
    H264 NALU分析
    微服务框架 SpringCloud微服务架构 8 Gateway 网关 8.7 网关的cors 跨域配置
    一文搞懂什么是@Component和@Bean注解以及如何使用
    Day51——JavaScript事件绑定,jQuery类库
  • 原文地址:https://blog.csdn.net/ttxiaoxiaobai/article/details/132859893