用不惯cJSON所以就自己编了个类,用起来舒服点

p.s.有些有点奇怪的bug,不会改,所以就注释掉了,如果可以的话麻烦大佬看一下。
p.s.功能不全

#ifndef DCJSON_H_
#define DCJSON_H_
#include<string>
#include<vector>
#include "cJSON.c"
namespace DcJSON
{
	// extern typedef class ARRAY;
	struct HashItem
	{
		std::string key;
		std::string value;
	};
	typedef std::vector<HashItem> HashTable;

	class JSON
	{
	  private:
		cJSON * _json;
	  public:
		JSON()
		{
			_json = cJSON_CreateObject();
		}
		JSON(std::string json)
		{
			cJSON_Parse(json.c_str());
		}
		 ~JSON()
		{
			cJSON_Delete(_json);
		}
		cJSON *toCJSON()
		{
			return _json;
		}
		void addInt(std::string key, int value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNumber(value));
		}
		void addDouble(std::string key, double value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNumber(value));
		}
		void addString(std::string key, std::string value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateString(value.c_str()));
		}
		void addBool(std::string key, cJSON_bool value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateBool(value));
		}
		void addNull(std::string key)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNull());
		}
		void addIntArray(std::string key, int *array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateIntArray(array, num));
		}
		void addDoubleArray(std::string key, double *array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateDoubleArray(array, num));
		}
		void addStringArray(std::string key, char **array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateStringArray(array, num));
		}
		void addObject(std::string key, JSON json)
		{
			cJSON_AddItemToObject(_json, key.c_str(), json.toCJSON());
		}
		/* void addArray(std::string key, const ARRAY array) {
		   cJSON_AddItemToObject(_json, key.c_str(), array.toCJSON()); } */
		void addHashItem(HashItem item)
		{
			cJSON_AddItemToObject(_json,item.key.c_str(),cJSON_CreateString(item.value.c_str()));
		}
		int getInt(std::string key)
		{
			return int (cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		double getDouble(std::string key)
		{
			return double (cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		std::string getString(std::string key)
		{
			return std::string(cJSON_GetStringValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		bool getBool(std::string key)
		{
			return bool(cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		HashItem toHashItem(std::string key)
		{
			HashItem result;
			result.key = key;
			result.value = this->getString(key);
			return result;
		}
		bool isNumber(std::string key)
		{
			return cJSON_IsNumber(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isString(std::string key)
		{
			return cJSON_IsString(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isObject(std::string key)
		{
			return cJSON_IsObject(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isFalse(std::string key)
		{
			return cJSON_IsFalse(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isTrue(std::string key)
		{
			return cJSON_IsTrue(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isNull(std::string key)
		{
			return cJSON_IsNull(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isArray(std::string key, int num)
		{
			return cJSON_IsArray(cJSON_GetArrayItem(_json, num));
		}
		friend std::ostream & operator<<(std::ostream & os, JSON & json)
		{
			os << cJSON_Print(json.toCJSON());
			return os;
		}
	};

	class ARRAY
	{
	  private:
		cJSON * _array;
	  public:
		ARRAY()
		{
			_array = cJSON_CreateArray();
		}
		ARRAY(std::string array)
		{
			cJSON_Parse(array.c_str());
		}
		~ARRAY()
		{
			cJSON_Delete(_array);
		}
		cJSON *toCJSON()
		{
			return _array;
		}
		void addInt(int value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNumber(value));
		}
		void addDouble(double value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNumber(value));
		}
		void addString(std::string value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateString(value.c_str()));
		}
		void addBool(cJSON_bool value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateBool(value));
		}
		void addNull()
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNull());
		}
		void addIntArray(int *array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateIntArray(array, num));
		}
		void addDoubleArray(double *array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateDoubleArray(array, num));
		}
		void addStringArray(char **array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateStringArray(array, num));
		}
		void addObject(JSON json)
		{
			cJSON_AddItemToArray(_array, json.toCJSON());
		}
		/*void addArray(const ARRAY array)
		{
			cJSON_AddItemToArray(_array, array.toCJSON());
		}*/
		void addHashItem(HashItem item)
		{
			JSON json;
			json.addString(item.key,item.value);
			cJSON_AddItemToArray(_array,json.toCJSON());
		}
		int getInt(int index)
		{
			return int (cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		double getDouble(int index)
		{
			return double (cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		std::string getString(int index)
		{
			return std::string(cJSON_GetStringValue(cJSON_GetArrayItem(_array, index)));
		}
		bool getBool(int index)
		{
			return bool(cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		bool isNumber(int index)
		{
			return cJSON_IsNumber(cJSON_GetArrayItem(_array, index));
		}
		bool isString(int index)
		{
			return cJSON_IsString(cJSON_GetArrayItem(_array, index));
		}
		bool isObject(int index)
		{
			return cJSON_IsObject(cJSON_GetArrayItem(_array, index));
		}
		bool isFalse(int index)
		{
			return cJSON_IsFalse(cJSON_GetArrayItem(_array, index));
		}
		bool isTrue(int index)
		{
			return cJSON_IsTrue(cJSON_GetArrayItem(_array, index));
		}
		bool isNull(int index)
		{
			return cJSON_IsNull(cJSON_GetArrayItem(_array, index));
		}
		bool isArray(int index)
		{
			return cJSON_IsArray(cJSON_GetArrayItem(_array, index));
		}
		friend std::ostream & operator<<(std::ostream & os, ARRAY & array)
		{
			os << cJSON_Print(array.toCJSON());
			return os;
		}
	};
}

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用cJson库来读取和修改JSON文件中的值,而不改变其格式。首先,你需要使用cJson库中的函数将JSON文件读入内存中的cJson对象中。然后,你可以使用cJson提供的函数来获取和修改JSON对象中的值。 以下是一个示例代码,该代码演示了如何使用cJson来读取JSON文件并替换其中的一个值: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" int main() { // 读取JSON文件 FILE *fp = fopen("example.json", "rb"); if (fp == NULL) { printf("Failed to open file\n"); return 1; } fseek(fp, 0, SEEK_END); long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); char *json_str = (char *)malloc(file_size + 1); fread(json_str, 1, file_size, fp); fclose(fp); // 将JSON文件解析为cJson对象 cJSON *root = cJSON_Parse(json_str); free(json_str); // 获取需要修改的值 cJSON *value = cJSON_GetObjectItem(root, "key"); // 替换值 cJSON_ReplaceItemInObject(root, "key", cJSON_CreateString("new_value")); // 将修改后的JSON对象转换为字符串 char *new_json_str = cJSON_Print(root); printf("%s\n", new_json_str); free(new_json_str); // 释放内存 cJSON_Delete(root); return 0; } ``` 在上面的示例中,我们首先使用`fopen`函数打开JSON文件,然后使用`fread`函数将文件内容读入内存。接下来,我们使用`cJSON_Parse`函数将JSON字符串解析为cJson对象。然后,我们使用`cJSON_GetObjectItem`函数获取需要修改的值,并使用`cJSON_ReplaceItemInObject`函数将其替换为新值。最后,我们使用`cJSON_Print`函数将修改后的JSON对象转换为字符串,并输出到控制台。最后,我们使用`cJSON_Delete`函数释放内存并删除cJson对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值