读取配置(初稿)

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "json/json.h"

using namespace std;

struct SStudent
{
    int id;        //编号
    string name;    //姓名
    int age;    //年龄
    string address; //地址
};
typedef vector<SStudent> SeqSStudent; 

/*
void fromJs( SStudent& stu, std::string js )
{
    Json::Value json;
    Json::Reader reader;
    reader.parse( js.c_str(), json );
    
    stu.id = json["id"].asInt();
    stu.name = json["name"].asString();
    stu.age  = json["age"].asInt();
    stu.address = json["address"].asString();    
} 
*/

void fromJs( SStudent& stu, Json::Value jv)
{
    stu.id = jv["id"].asInt();
    stu.name = jv["name"].asString();
    stu.age = jv["age"].asInt();
    stu.address = jv["address"].asString();

}

void loadFile(const std::string& file, SeqSStudent& seq)
{
    Json::Value json;
    Json::Reader reader;

    ifstream ifs ( file.c_str() );
    reader.parse( ifs, json );
    SStudent stu;
    for( Json::Value::UInt i = 0; i < json.size(); ++ i )
    {
        fromJs( stu, json[i] );
        seq.push_back( stu );    
    }
}


int main(int argc, char* argv[])
{
    SeqSStudent seqStu;
    loadFile("student.json", seqStu);    
    
    cout << "begin print" << endl;
    
    for(SeqSStudent::const_iterator cit = seqStu.begin(); cit != seqStu.end(); ++cit)
    {
        cout << "id:" << cit->id << "\tname: " << cit->name << "\tage: " << cit->age << "\tAddress: " << cit->address << endl;;
    }
    
    cout << "end print" << endl;

    return 0;
}


转载于:https://my.oschina.net/jianchaopeng/blog/280397

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值