标签归档:c++

JsonCPP 解析与对象生成

安装JsonCPP

vcpkg install jsoncpp

CMake文件添加

find_package(jsoncpp CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE JsonCpp::JsonCpp)

解析Json

#include <iostream>
#include "json/reader.h"
#include "json/value.h"

using namespace std;


int main(int argc, char **argv) {

    std::string strJson = R"({"id":4,"name":"Chad","scores":2.99})";
    Json::Reader reader;
    Json::Value jvRoot;
    if (reader.parse(strJson, jvRoot)) {
        int nID = jvRoot["id"].asInt();
        std::string strName = jvRoot["name"].asString();
        double dScores = 0;
        
        if (jvRoot.isMember("scores")) { // 判断是否存在
            dScores = jvRoot["scores"].asDouble();
        }

        cout << nID << ";" << strName << ";" << dScores << endl;
    }
    return 0;
}


存储Json

#include <iostream>
#include <fstream>
#include "json/writer.h"
#include "json/reader.h"
#include "json/value.h"

using namespace std;

int writeJson() {
    Json::Value root;
    Json::Value data;
    root["code"] = 1001;
    root["message"] = "No permission";
    if (!data.empty()) { // data可以是m_data成员变量
        root["data"] = data;
    }

    // 保存为字符串
    string tmp(root.toStyledString());
    cout << tmp << endl;

    // 写入文件
    std::ofstream ofs("example.json");
    Json::StreamWriterBuilder builder;
    std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
    if (writer->write(root, &ofs)) {
        cout << "保存成功" << endl;
    } else {
        cout << "保存失败" << endl;
    }
    return 0;
}

char*,char[], string 之间互相转换

char[] 转 char*

char ch[]="abcdef";
char *s = ch;
cout << s << endl; // 这不是打印地址,而是打印s的内容

char*转char[]

#include <string>
#include <iostream>

using namespace std;

int main() {

    char *s = (char *) "aabbcc";
    char ch[100];
    size_t size = sizeof(char) * strlen(s) + 1;
    strncpy_s(ch, s, size);

    cout << ch << endl;

    return 0;
}

string转char[]

#include <string>
#include <iostream>

using namespace std;

int main() {

    string s = "abc";
    // to char[]
    const size_t chSize = 128;
    char ch[chSize];
    int i = 0;
    strncpy_s(ch, chSize, s.c_str(), s.size());
    cout << ch << endl;

    // to const char*
    const char *aa = s.c_str();
    cout << aa << endl;

    return 0;
}

char[]转string

#include <string>
#include <iostream>

using namespace std;

int main() {


    string s;
    char ch[10] = "abcdef";
    s = ch; // 简单粗爆,

    string b(ch);   // 构造函数

    string c;
    c.append(ch); // append

    cout << s << endl;
    cout << b << endl;
    cout << c << endl;
}

string转char*

#include <string>
#include <iostream>

using namespace std;

int main() {


    // 方法一
    {
        string s = "aa";
        char *ch;
        ch = (char *) s.c_str();
        cout << ch << endl;
    }

    
    // 方法二
    {
        string s = "bb";
        char *ch;
        s += '\0';
        ch = (char *) s.data();
        cout << ch << endl;
    }
}

char*转string

#include <string>
#include <iostream>

using namespace std;

int main() {

    // 方法一
    {
        string s;
        char *ch = (char *) "abcdef";
        s = ch;

        cout << s << endl;
    }

    // 方法二
    {
        string s;
        char *ch = (char *) "abcdef";
        s = ch;
        s.assign(ch);

        cout << s << endl;
    }
}