jsconcpp 序列化json配置文件

123.json配置文件如下:

{
    "ID": 20,
    "longitude": 112.12345,
    "latitude": 28.67890,
    "num": 123,
    "openFlag": true,
    "name": "xi xi",
    "data": [4, 2, 3, 4, 5]
}

序列化代码如下:

#include "../common.h"
#include <jsoncpp/json/json.h>

int main()
{
    ifstream readFile("123.json",ios::binary);
    if(!readFile.is_open())
    {
        printf("open fail\n");
        return -1;
    }
    else
    {
        Json::Reader reader;
        Json::Value root;
        if(reader.parse(readFile,root))  //reader将文件中的内容解析到root
        {
            cout << "ID: " << root["ID"].asInt() << endl;
            cout << "longitude: " << root["longitude"].asFloat() << endl;
            cout << "latitude: " << root["latitude"].asFloat() << endl;
            cout << "openFlag: " << root["openFlag"].asBool() << endl;
            cout << "name: " << root["name"].asString() << endl;
            for(int i = 0; i < root["data"].size(); ++i)
            {
                printf("data[%d]: %d\n",i,root["data"][i].asInt());
            }
        }
        else
        {
            cout << "no reader" << endl;
        }
        readFile.close();
        return 0;
    }
}

编译的时候需要连接 jsoncpp:

g++ 1.cpp -ljsoncpp -std=c++11

结果如下:

ID: 20
longitude: 112.123
latitude: 28.6789
openFlag: 1
name: xi xi
data[0]: 4
data[1]: 2
data[2]: 3
data[3]: 4
data[4]: 5

稍微复杂一点的也是如此,实际上C++使用json与使用数组的方式差不多:

#include "../common.h"

int main()
{
    ifstream readFile("456.json",ios::binary);
    if(!readFile.is_open())
    {
        printf("open fail\n");
        return -1;
    }
    else
    {
        Json::Reader reader;
        Json::Value root;
        if(reader.parse(readFile,root))  //reader将文件中的内容解析到root
        {
            cout << "devMessage_UUID: " << root["devMessage"]["UUID"].asString() << endl;
            cout << "devMessage_stationName: " << root["devMessage"]["stationName"].asString() << endl;
            cout << "devMessage_longitude: " << root["devMessage"]["longitude"].asFloat() << endl;
            cout << "devMessage_latitude: " << root["devMessage"]["latitude"].asFloat() << endl;
            cout << "devMessage_AntennaShape: " << root["devMessage"]["AntennaShape"].asInt() << endl;
            cout << "devMessage_TimeZone: " << root["devMessage"]["TimeZone"].asInt() << endl;
            cout << endl;

            cout << "softConfig_autoStart: " << root["softConfig"]["autoStart"].asBool() << endl;
            cout << "softConfig_autoRest: " << root["softConfig"]["autoRest"].asBool() << endl;
            cout << "softConfig_initPhase433: [";
            for(int i = 0; i < root["softConfig"]["initPhase433"].size(); ++i)
            {
                if(i == root["softConfig"]["initPhase433"].size() - 1)
                {
                    cout <<  root["softConfig"]["initPhase433"][i].asFloat() << "]" << endl;
                }
                else
                {
                    cout <<  root["softConfig"]["initPhase433"][i].asFloat() << " ,";
                }
                
            }
            cout << "softConfig_d800: " << root["softConfig"]["d800"].asFloat() << endl;
            
        }
        else
        {
            cout << "no reader" << endl;
        }
        readFile.close();
        return 0;
    }
}

456.json:

{
    "devMessage": {
        "UUID": "json",
        "stationName": "station",
        "longitude": 112.123456,
        "latitude": 28.789012,
        "AntennaShape": 3,
        "TimeZone": 8
    },
    "softConfig": {
        "autoStart": true,
        "autoRest": true,
        "initPhase433": [-0.9, -0.8, -1.9, -2.9],
        "d800": 0.15
    }
}

结果如下:

devMessage_UUID: json
devMessage_stationName: station
devMessage_longitude: 112.123
devMessage_latitude: 28.789
devMessage_AntennaShape: 3
devMessage_TimeZone: 8

softConfig_autoStart: 1
softConfig_autoRest: 1
softConfig_initPhase433: [-0.9 ,-0.8 ,-1.9 ,-2.9]
softConfig_d800: 0.15

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值