C/C++生成JSON文件

前面我们介绍了 JSON文件是个什么样子,那么在编程中我们怎么用呢?

C/C++怎么生成JSON文件格式呢?

 

这里我们就是用一个开源库 cJSON(方法多种多样也可以用其他的库)来生成一个json文件。

如下是我们要生成的一个json文件

{
	"roomNumber" : "B06",          // 宿舍编号
	"peopleCount" : 6,				// 宿舍人数
    "roomFreeTabels" : 0,			// 宿舍空闲床位
    "roomElemName" : [				// 宿舍人的姓名
    	"libero", 
    	"rock",
    	"martin",
    	"sky",
    	"bingo",
    	"janny"
    ]
}

代码Demo

#include <iostream>
#include <Windows.h>
#include <fstream>
#include "cJSON.h"

using namespace std;

#define _CRT_SECURE_NO_WARNINGS
#define OPEN_MAX_TIMES 5

int main(int argc, const char* argv[])
{
	cJSON* root = cJSON_CreateObject();
	cJSON_AddItemToObject(root, "roomNumber", cJSON_CreateString("B06"));
	cJSON_AddItemToObject(root, "peopleCount", cJSON_CreateNumber(6));
	cJSON_AddItemToObject(root, "roomFreeTabels", cJSON_CreateNumber(0));

	// 姓名数组 这里可以用循环代替 这样一个个写太low了
	// 但是为了让大家更清晰的看还是一个个写
	cJSON* array = cJSON_CreateArray();
	cJSON_AddItemToArray(array, cJSON_CreateString("libero"));
	cJSON_AddItemToArray(array, cJSON_CreateString("rock"));
	cJSON_AddItemToArray(array, cJSON_CreateString("martin"));
	cJSON_AddItemToArray(array, cJSON_CreateString("sky"));
	cJSON_AddItemToArray(array, cJSON_CreateString("bingo"));
	cJSON_AddItemToArray(array, cJSON_CreateString("janny"));

	// 把数组加紧 大的对象中
	cJSON_AddItemToObject(root, "roomElemName", array);

	// 写道文件中去
	char* buf = cJSON_Print(root);

	ofstream fout;
	fout.open("cjosn.json", ios_base::out | ios_base::binary);  // 二进制的写
	int count = 0;
	while (!fout.is_open() && count < OPEN_MAX_TIMES) {
		cerr << "open file error.has " << OPEN_MAX_TIMES - (++count) 
			<< "times retry" << endl;
	}

	do{
		if (count == 5) {
			cerr << "file open error total" << endl;
			break;
		}
		fout << buf << flush;
		fout.close();
	} while (0);

	cJSON_Delete(root);

	system("pause");
	return 0;
}

生成的文件:

 

 

 

 

 

 

 

更多的关于数据库学习我会在下面的文章中陆续的分享,也可以关注‘奇牛学院’

来一起讨论

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,RapidJSON是一个快速、轻量级的JSON库,它非常适合处理JSON数据。如果你想使用RapidJSON生成JSON文件,你需要包含`rapidjson/document.h`和`rapidjson/writer.h`头文件,并创建一个`Document`对象来构建JSON内容,然后使用`Writer`来写入文件。 以下是一个简单的示例,展示如何生成JSON文件并设置指定路径: ```cpp #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <fstream> #include <string> using namespace rapidjson; // 创建JSON数据 Document d; d.SetObject(); d.AddMember("key1", "value1", d.GetAllocator()); d.AddMember("key2", 42, d.GetAllocator()); // 设置文件路径 std::string filePath = "path/to/your/json/file.json"; // 创建文件流 std::ofstream file(filePath); // 创建Writer并写入文件流 if (file.is_open()) { Writer<fstream> writer(file); writer.write(d.Accept(writer)); file.close(); } else { // 处理文件打开失败的情况 std::cerr << "Failed to open file for writing." << std::endl; } // 修改JSON文件名(如果需要在程序运行时更改) std::string newFileName = "new_json_file.json"; std::string newFilePath = "path/to/new/" + newFileName; std::ifstream oldFile(filePath); std::ofstream newFile(newFilePath); if (oldFile.is_open() && newFile.is_open()) { std::copy(std::istreambuf_iterator<char>(oldFile), {}, newFile); oldFile.close(); newFile.close(); std::remove(filePath.c_str()); // 删除旧文件 } ``` 在这个例子中,我们首先创建了JSON文档并添加了一些键值对。然后,我们指定了一个文件路径,并尝试打开文件进行写入。如果文件成功打开,我们将JSON内容写入到文件中。最后,我们展示了如何在运行时更改文件名,但这需要先读取原文件内容,再写入新文件,然后删除旧文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值