json库 rapidjson的简单使用

json库 rapidjson的简单使用

读取json文件到rapidjson格式

#include "rapidjson/document.h"    // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON


rapidjson::Document readJson(const char* jsonFile)
{
    std::ifstream t;
    long long length = 0;
    t.open(jsonFile); // open input file
    if (!t.is_open())
    {
        LOGGING_ERROR("Config file open error, file path : %s", jsonFile);
    }

    t.seekg(0, std::ios::end); // go to the end
    length = t.tellg(); // report location (this is the length)
    t.seekg(0, std::ios::beg);// go back to the beginning

    char* configData = new char[length];// allocate memory for a buffer of appropriate dimension
    if (!configData)
    {
        LOGGING_ERROR("Config file new memory error");
    }

    t.read(configData, length);// read the whole file into the buffer
    t.close();// close file handle

    rapidjson::Document configDocument;
    configDocument.Parse<rapidjson::kParseStopWhenDoneFlag>((const char*)configData);
    if (!configDocument.IsObject())
    {
        LOGGING_ERROR("%s content is not json type", jsonFile);
    }

    free(configData);

    return configDocument;
}

保存数据到接送文件

#include "rapidjson/document.h"    // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON

int saveToFile(std::string filename)
{
 	rapidjson::StringBuffer s;
    //rapidjson::Writer<rapidjson::StringBuffer> writer(s); //不使用格式化输出
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);  //使用格式化输出
	
	writer.StartObject();
    writer.Key("rows");
    writer.Int(3);
    writer.Key("cols");
    writer.Int(4);
   
    {
        writer.Key("arr_name"); //object中任何值都必须有名字
        writer.StartArray();
        for (size_t i = 0; i < 5; i++)
        {
        		writer.Uint((unsigned int)i);
                writer.StartObject();
                writer.Key("index");
                writer.Uint((unsigned int)i);
                writer.EndObject();
        }
        writer.EndArray();
    }

	 writer.EndObject();
	 
    std::ofstream outfile(jsonFile,std::ios::trunc);
    outfile << s.GetString();
    outfile.close(); 
 
    return 0;
}

从文件读是Jon数据

  int readMesh(const char* mesh_json)
  {
        rapidjson::Document configDocument = readJson(mesh_path);
        if (!configDocument.HasMember("mat_cols") || !configDocument.HasMember("mat_rows"))
        {
            prinrf("ERROR: %s file no mat_cols or mat_rows", mesh_path);
            return -1;
        }
		
		int mat_cols = configDocument["mat_cols"].GetInt();
        int mat_rows = configDocument["mat_rows"].GetInt();

		rapidjson::Document::Array local_rects_document = configDocument["loc_rects"].GetArray();
        for (unsigned int i = 0; i < local_rects_document.Size(); i++)
        {
            int ndex     = local_rects_document[i]["index"].GetInt();
            double start_x   = local_rects_document[i]["x0"].GetDouble();
            double start_y   = local_rects_document[i]["y0"].GetDouble();
            double end_x     = local_rects_document[i]["x1"].GetDouble(); 
            double end_y     = local_rects_document[i]["y1"].GetDouble();
        }
        
  }

官方网站: http://rapidjson.org/md_doc_tutorial.html

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读