C++使用jsoncpp使class、struct转json

写在前面:使用mingw编译jsoncpp,使用前,用CMakeList.txt链接上,如果使用vs2017,改链接库就是了。

先上结果

C++使用jsoncpp使class、struct转json

C++使用jsoncpp使class、struct转json

上代码

CMakeList.txt

cmake_minimum_required(VERSION 3.13)
project(Class_to_json)
set(INC_DIR  JSONCPP/include)
set(LINK_DIR  JSONCPP/lib)

include_directories(${INC_DIR})
link_directories(${LINK_DIR})
link_libraries(libjsoncpp.a)

set(CMAKE_CXX_STANDARD 14)
add_executable(Class_to_json main.cpp to_json.h)
target_link_libraries(Class_to_json libjsoncpp.a)

to_json.h

//
// Created by haosir on 2019/2/27.
//

#ifndef CLASS_TO_JSON_TO_JSON_H
#define CLASS_TO_JSON_TO_JSON_H
#include <iostream>
#include <vector>
#include <json/json.h>
struct person{
    person(std::string name, long long phone) : name(name), phone(phone){};
    std::string name;
    int64_t phone;
};//作为json类 数组test
class school{
public:
    school();
    ~school();
    std::string to_json();//转化成json存储在string中
    void set_name(const std::string &);
    void set_id(const double &);
    void set_eamil(const int64_t &);
    void set_one(const person &);
    void set_two(const person &);
    void set_others(const std::vector<std::string> &);
    /*设置类函数*/
private:
    /*设置为指针形式, 在转化为json的时候判断是否为空,为空,则转化为null*/
    std::string *name;
    double *id;
    int64_t *eamil;
    person *one;
    person *two;
    std::vector<std::string> *others;
};
school::school():name(nullptr), id(nullptr), eamil(nullptr), one(nullptr), two(nullptr), others(nullptr){};
school::~school(){
    delete(name);
    delete(id);
    delete(eamil);
    delete(one);
    delete(two);
    delete(others);
};
std::string school::to_json(){
    std::string str;
    Json::Value root;
    Json::Value person1;
    Json::Value person2;
    Json::Value the_others;

    if(one != nullptr) {
        person1["name"] = one->name;
        person1["phone"] = one->phone;
        root["one"] = person1;
    }
    else
        root["one"] = Json::Value::null;//转化为null,自带的格式

    if(two != nullptr) {
        person2["name"] = one->name;
        person2["phone"] = one->phone;
        root["two"] = person2;
    }
    else
        root["two"] = Json::Value::null;

    if(others != nullptr) {
        for (auto it : *others) {
            the_others.append(it.c_str());
        }
        root["others"] = the_others;
    }
    else
        root["others"] = Json::Value::null;

    if(name != nullptr)
        root["name"] = *name;
    else
        root["name"] = Json::Value::null;

    if(id != nullptr)
        root["id"] = *id;
    else
        root["id"] = Json::Value::null;

    if(eamil != nullptr)
        root["eamil"] = eamil;
    else
        root["eamil"] = Json::Value::null;
    str = root.toStyledString();
    return str;
}
void school::set_name(const std::string & input) {
    if(name == nullptr)
        name = new std::string(input);
    else
        *name = input;
}
void school::set_id(const double & input) {
    if(id == nullptr)
        id = new double(input);
    else
        *id = input;
}
void school::set_eamil(const int64_t & input) {
    if(eamil == nullptr)
        eamil = new int64_t(input);
    else
        *eamil = input;
}
void school::set_one(const person & input) {
    if(one == nullptr)
        one = new person(input);
    else
        *one = input;
}
void school::set_two(const person & input) {
    if(two == nullptr)
        two = new person(input);
    else
        *two = input;
}
void school::set_others(const std::vector<std::string> & input) {
    if(others == nullptr)
        others = new std::vector<std::string>(input);
    else
        *others = input;
}
#endif //CLASS_TO_JSON_TO_JSON_H

test.cpp

#include "to_json.h"

int main()
{
    school a;
    a.set_eamil(468464);
    a.set_name("sdjauhdash");
    a.set_one(person("asda", 56465844));
    a.set_others(std::vector<std::string>{"asdasd","sadasdasdas"});
    std::cout << a.to_json() << std::endl;
}