首页 > Web开发 > 详细

JSONCPP学习笔记

时间:2018-05-25 11:51:18      阅读:185      评论:0      收藏:0      [点我收藏+]

基本使用

使用jsoncpp库解析、修改、打印JSON串

源文件

$ cat main.cpp
#include <iostream>
#include "json/json.h"

using namespace std;

const char *style = "{     \"encoding\" : \"UTF-8\",     \"plug-ins\" : [         \"python\",         \"c++\",         \"ruby\"         ],     \"indent\" : {\"length\" : 3, \"use_space\": true } }";

int main()
{
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(style, root))
    {
        cout << "parse fails." << endl;
        return -1;
    }

    // 基础数据的读取
    std::string encoding = root.get("encoding", "GBK" ).asString();
    cout << "encoding : " << encoding << endl;
    // 列表数据的读取
    const Json::Value plugins = root["plug-ins"];
    cout << "plug-ins : [ ";
    for ( int index = 0; index < plugins.size(); ++index )
    {
        cout << plugins[index].asString();
        if (index != plugins.size())
            cout << ", ";
    }
    cout << " ]" << endl;
    // 对象数据的读取和修改
    root["indent"]["length"] = 4;
    root["indent"]["use_space"] = false;

    // 打印修改后的JSON串
    cout << "Json after modify:" << endl;
    Json::StyledStreamWriter output;
    output.write(std::cout, root);
}

JSONCPP学习笔记

原文:https://www.cnblogs.com/silvermagic/p/9087635.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!