linux系统cpp使用libjson库

⌚Time: 2023-05-02 18:13:24

👨‍💻Author: Jack Ge

安装libjson开发库


sudo apt-get install libjsoncpp-dev

测试程式


#include <json/json.h>

#include <iostream>

using namespace std;

using namespace Json;



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

    //根节点

    Json::Value json_aaa;

    //添加节点属性

    json_aaa["role"] = "Bus Driver";

    //子节点

    Json::Value json_time;

    //子节点属性

    json_time["hours"] = 2;

    json_time["minutes"] = 3;

    json_time["seconds"] = 40;

    Json::Value json_location;

    json_location["location_name"] = "room4";

    json_location["latitude"] = 22.3451f;

    json_location["longitude"] = 132.3321f;

    //挂载子节点

    json_aaa["time"] = json_time;

    json_aaa["location"] = json_location;

    //添加json数组

    Json::Value json_things(Json::arrayValue);

    for(int i=0; i<2; i++){

        Json::Value json_thing;

        json_thing["q1w"] = i;

        json_thing["q2w"] = "zxs";

        json_things.append(json_thing);

    }

    json_aaa["things"] = json_things;



    //输出所有节点

    cout<<json_aaa.toStyledString()<<endl;

    //输出根节点

    cout<<"role:"<<json_aaa["role"].asString()<<endl;

    //输出子节点

    cout<<"hours::"<<json_aaa["time"]["hours"].asInt()<<endl;

    cout<<"lication_name:"<<json_aaa["location"]["location_name"].asString()<<endl;

    //输出数组

    for(int i=0; i<json_aaa["things"].size(); i++){

        cout<<json_aaa["things"][i]["q2w"].asString()<<" ";

    }

    cout<<endl;

    return 0;

}


编译链接参数


g++ t.cpp -Wall -std=c++11 -I/usr/include/jsoncpp -ljsoncpp

输出结果


{

   "location" : {

      "latitude" : 22.345100402832031,

      "location_name" : "room4",

      "longitude" : 132.33210754394531

   },

   "role" : "Bus Driver",

   "things" : [

      {

         "q1w" : 0,

         "q2w" : "zxs"

      },

      {

         "q1w" : 1,

         "q2w" : "zxs"

      }

   ],

   "time" : {

      "hours" : 2,

      "minutes" : 3,

      "seconds" : 40

   }

}



role:Bus Driver

hours::2

lication_name:room4

zxs zxs 





从字符串读取json并保存到文件


#include <json/json.h>

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

using namespace Json;

int main(){

    //从字符串解析json

    string str = "{\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":333}";

    Json::Reader reader;

    Json::Value value;

    if(reader.parse(str,value)){

        cout<<value["aaa"].asString()<<" "

            <<value["bbb"].asString()<<" "

            <<value["ccc"].asInt()<<" "

            <<endl;

    }

    //输出json到文件

    Json::StyledWriter sWriter;

    ofstream oFile("json.txt",ios::out);

    if(oFile.is_open()){

        oFile<<sWriter.write(value);

    }

    oFile.close();

    return 0;

}


文件内容

json.txt




{

   "aaa" : "111",

   "bbb" : "222",

   "ccc" : 333

}

从文件读取json

json1.txt


{

   "achievements" : ["ach1","ach2","ach3"],

   "age" : 22,

   "name" : "john",

   "partner" : 

   {

      "partner_age" : 21,

      "partner_name" : "mike",

      "partner_sex_is_male" : false

   },

   "sex_is_male" : true

}



代码


#include <json/json.h>

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

using namespace Json;

int main(){

    ifstream iFile("json1.txt", ios::in);

    if(iFile.is_open()){

        Json::Reader reader;

        Json::Value value;

        if(reader.parse(iFile, value)){

            cout<<"my name is "<<value["name"].asString()<<endl;

            cout<<"i am "<<value["age"].asInt()<<" years old"<<endl;

            cout<<"i am a "<<(value["sex_is_male"].asBool()?"man":"woman")<<endl;

            cout<<"my partner's name is "<<value["partner"]["partner_name"].asString()<<endl;

            cout<<"my partner is "<<value["partner"]["partner_age"].asInt()<<" years old"<<endl;

            cout<<"here is my achievements:"<<endl;

            for(int i=0; i<value["achievements"].size(); i++){

                cout<<value["achievements"][i].asString()<<" ";

            }

            cout<<endl;

        }

    }



    iFile.close();

    return 0;

}


输出结果


my name is john

i am 22 years old

i am a man

my partner's name is mike

my partner is 21 years old

here is my achievements:

ach1 ach2 ach3 





判断字段存在的办法


if (value["aa"].type() != Json::nullValue){

    //aa字段存在

}

if(value["bb"].isNull()){

    //bb字段存在

}

判断字段类型


if (value["aa"].isString()){

    //aa字段是字符

}

if (value["aa"].isInt()){

    //aa字段是数值

}

if (value["aa"].iArray()){

    //aa字段是数组

}