C++应用之解析json

多个json库比较:https://www.oschina.net/news/61942/cpp-json-compare

符合标准程度(越高越好):

Conformance

解析至DOM的时间(越低越好):Parsing Time解析至DOM后的内存(越低越好):

Parsing Time把DOM生成JSON的时间(越低越好):

把DOM生成含换行及缩进的JSON的时间(越低越好):

Prettify Time

可执行文件(把JSON解析至DOM,然后统计JSON类型)的大小(越低越好):Code Size

推荐Rapidjson: https://www.oschina.net/p/rapidjson
// rapidjson/example/simpledom/simpledom.cpp`
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;

int main() {
    // 1. Parse a JSON string into DOM.
    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
    Document d;
    d.Parse(json);

    // 2. Modify it by DOM.
    Value& s = d["stars"];
    s.SetInt(s.GetInt() + 1);

    // 3. Stringify the DOM
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    // Output {"project":"rapidjson","stars":11}
    std::cout << buffer.GetString() << std::endl;
    return 0;
}

但方便性来说boost是最方便的:

官方文档:http://www.ce.unipr.it/~medici/boost_ptree.html

简单使用:https://blog.csdn.net/hzyong_c/article/details/7163589

多用途:https://blog.csdn.net/ufe_1/article/details/8032089

#include <boost/property_tree/ptree.hpp>  
#include <boost/property_tree/json_parser.hpp>  

const std::string file_path = "C:\\test.txt";

void generate_user()
{
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;

    boost::property_tree::ptree item1;
    item1.put("ID", "1");
    item1.put("Name", "wang");
    items.push_back(std::make_pair("1", item1));

    boost::property_tree::ptree item2;
    item2.put("ID", "2");
    item2.put("Name", "zhang");
    items.push_back(std::make_pair("2", item2));

    boost::property_tree::ptree item3;
    item3.put("ID", "3");
    item3.put("Name", "li");
    items.push_back(std::make_pair("3", item3));

    root.put_child("user", items);
    boost::property_tree::write_json(file_path, root);
}

void read_user()
{
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;
    boost::property_tree::read_json<boost::property_tree::ptree>(file_path, root);


    items = root.get_child("user");
    for (boost::property_tree::ptree::iterator it = items.begin(); it != items.end(); ++it)
    {
        //遍历读出数据  
        string key = it->first;//key ID  
        string ID = it->second.get<string>("ID");
        string Name = it->second.get<string>("Name");
    }
}

{
    "user": {
        "1": { "ID": "1", "Name" : "wang"},
            "2" : { "ID": "2", "Name" : "zhang"},
            "3" : { "ID": "3", "Name" : "li"}
    }
}

//将json串写入string    
boost::property_tree::ptree item;
item.put("a", 2);
std::stringstream is;
boost::property_tree::write_json(is, item);
std::string s = is.str();

//从string中解析json串  
std::string c;//c为json串  
std::istringstream iss;
iss.str(c);
boost::property_tree::ptree item;
boost::property_tree::json_parser::read_json(iss, item);
int n = item.get<int>("a");

/*
1. 用boost::property_tree解析字符串遇到"\/"时解析失败,而jsoncpp可以解析成功,要知道'/'前面加一个'\'是JSON标准格式。
2. boost::property_tree的read_json和write_json在多线程中使用会引起崩溃。
针对1,可以在使用boost::property_tree解析前写个函数去掉"\/"中的'\',针对2,在多线程中同步一下可以解决。
我的使用心得:使用boost::property_tree不仅可以解析json,还可以解析xml,info等格式的数据。对于解析json,
使用boost::property_tree解析还可以忍受,但解析xml,由于遇到问题太多只能换其它库了。
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值