cJSON 修改已有的键值对
在 cJSON 中,修改已有的键值对可以通过以下步骤实现:
-
找到需要修改的键值对。
-
根据值的类型(字符串、数字、布尔值等),使用相应的函数更新值。
以下是具体的实现方法和示例代码:
1. 修改字符串类型的值
使用 cJSON_ReplaceItemInObject 或直接修改 valuestring。
示例代码:
#include "cJSON.h"
#include <stdio.h>
void modify_json_string(cJSON* root, const char* key, const char* new_value) {
cJSON* item = cJSON_GetObjectItem(root, key);
if (item != NULL && cJSON_IsString(item)) {
cJSON_SetValuestring(item, new_value); // 直接修改字符串值
} else {
printf("Key not found or not a string!\n");
}
}
int main(void) {
const char* json_string = "{\"name\":\"Alice\",\"age\":25}";
cJSON* root = cJSON_Parse(json_string);
if (root == NULL) {
printf("JSON parse error!\n");
return 1;
}
modify_json_string(root, "name", "Bob");
char* modified_json = cJSON_Print(root);
printf("Modified JSON: %s\n", modified_json);
cJSON_Delete(root);
cJSON_free(modified_json);
return 0;
}
2. 修改数字类型的值
直接修改 valueint 或 valuedouble。
示例代码:
#include "cJSON.h"
#include <stdio.h>
void modify_json_number(cJSON* root, const char* key, int new_value) {
cJSON* item = cJSON_GetObjectItem(root, key);
if (item != NULL && cJSON_IsNumber(item)) {
item->valueint = new_value; // 修改整数值
item->valuedouble = (double)new_value; // 同时修改双精度值
} else {
printf("Key not found or not a number!\n");
}
}
int main(void) {
const char* json_string = "{\"name\":\"Alice\",\"age\":25}";
cJSON* root = cJSON_Parse(json_string);
if (root == NULL) {
printf("JSON parse error!\n");
return 1;
}
modify_json_number(root, "age", 30);
char* modified_json = cJSON_Print(root);
printf("Modified JSON: %s\n", modified_json);
cJSON_Delete(root);
cJSON_free(modified_json);
return 0;
}
3. 修改布尔类型的值
直接修改 valueint。
示例代码:
#include "cJSON.h"
#include <stdio.h>
void modify_json_bool(cJSON* root, const char* key, int new_value) {
cJSON* item = cJSON_GetObjectItem(root, key);
if (item != NULL && cJSON_IsBool(item)) {
item->valueint = new_value; // 修改布尔值
} else {
printf("Key not found or not a boolean!\n");
}
}
int main(void) {
const char* json_string = "{\"name\":\"Alice\",\"is_student\":true}";
cJSON* root = cJSON_Parse(json_string);
if (root == NULL) {
printf("JSON parse error!\n");
return 1;
}
modify_json_bool(root, "is_student", 0); // 修改为 false
char* modified_json = cJSON_Print(root);
printf("Modified JSON: %s\n", modified_json);
cJSON_Delete(root);
cJSON_free(modified_json);
return 0;
}
4. 修改嵌套的键值对
如果 JSON 数据是嵌套的,需要先找到嵌套的对象,然后再修改键值对。
示例代码:
#include "cJSON.h"
#include <stdio.h>
void modify_nested_json(cJSON* root, const char* parent_key, const char* key, const char* new_value) {
cJSON* parent = cJSON_GetObjectItem(root, parent_key);
if (parent != NULL && cJSON_IsObject(parent)) {
cJSON* item = cJSON_GetObjectItem(parent, key);
if (item != NULL && cJSON_IsString(item)) {
cJSON_SetValuestring(item, new_value); // 修改嵌套的字符串值
} else {
printf("Key not found or not a string!\n");
}
} else {
printf("Parent key not found or not an object!\n");
}
}
int main(void) {
const char* json_string = "{\"name\":\"Alice\",\"address\":{\"city\":\"New York\"}}";
cJSON* root = cJSON_Parse(json_string);
if (root == NULL) {
printf("JSON parse error!\n");
return 1;
}
modify_nested_json(root, "address", "city", "Los Angeles");
char* modified_json = cJSON_Print(root);
printf("Modified JSON: %s\n", modified_json);
cJSON_Delete(root);
cJSON_free(modified_json);
return 0;
}
5. 替换整个键值对
如果需要替换整个键值对(包括键和值),可以使用 cJSON_ReplaceItemInObject。
示例代码:
#include "cJSON.h"
#include <stdio.h>
void replace_json_item(cJSON* root, const char* key, cJSON* new_item) {
cJSON_ReplaceItemInObject(root, key, new_item);
}
int main(void) {
const char* json_string = "{\"name\":\"Alice\",\"age\":25}";
cJSON* root = cJSON_Parse(json_string);
if (root == NULL) {
printf("JSON parse error!\n");
return 1;
}
cJSON* new_item = cJSON_CreateString("Bob");
replace_json_item(root, "name", new_item);
char* modified_json = cJSON_Print(root);
printf("Modified JSON: %s\n", modified_json);
cJSON_Delete(root);
cJSON_free(modified_json);
return 0;
}
6. 注意事项
-
内存管理:如果使用
cJSON_ReplaceItemInObject,旧的值会被自动释放,无需手动调用cJSON_Delete。 -
类型检查:在修改值之前,务必检查值的类型(如
cJSON_IsString、cJSON_IsNumber等),避免类型不匹配导致的错误。 -
嵌套对象:修改嵌套对象时,需要先找到父对象,然后再修改子对象。
总结
通过以上方法,可以轻松修改 cJSON 中的键值对。根据值的类型(字符串、数字、布尔值等),选择合适的方式进行修改。如果需要替换整个键值对,可以使用 cJSON_ReplaceItemInObject。如果有更多问题,欢迎继续讨论!
| cJSON API | 说明 |
| cJSON_Version() | 获得cJSON的版本 |
| cJSON_InitHooks(); | 初始化cJSON_Hooks结构体 |
| cJSON_Parse(); | 将字符串解析成cJSON结构体 |
| cJSON_ParseWithOpts() | 使用一些配置解析字符串 |
| cJSON_Print() | 将cJSON结构体转换成格式化的字符串 |
| cJSON_PrintUnformatted() | 将cJSON结构体转换成未格式化的字符串 |
| cJSON_PrintBuffered() | 将cJSON结构体使用buffer的字符串,格式化可选 |
| cJSON_PrintPreallocated() | 将cJSON结构体使用预分配的内存的字符串,格式化可选 |
| cJSON_Delete() | 删除cJSON结构体 |
| cJSON_GetArraySize() | 返回Array类型的大小,对Object类型也是有效的 |
| cJSON_GetArrayItem() | 返回Array类型的index的值,对Object类型也有效 |
| cJSON_GetObjectItem() | 使用key获得对应的value |
| cJSON_GetObjectItemCaseSensitive() | 使用对大小写敏感的key获得对应的value |
| cJSON_HasObjectItem() | 判断是否ObjectItem存在 |
| cJSON_GetErrorPtr() | 获得错误信息 |
| 类型判断 | |
| cJSON_IsInvalid() | 类型判断 |
| cJSON_IsFalse() | 类型判断 |
| cJSON_IsTrue() | 类型判断 |
| cJSON_IsBool() | 类型判断 |
| cJSON_IsNull() | 类型判断 |
| cJSON_IsNumber() | 类型判断 |
| cJSON_IsString() | 类型判断 |
| cJSON_IsArray() | 类型判断 |
| cJSON_IsObject() | 类型判断 |
| cJSON_IsRaw() | 类型判断 |
| 创造对应类型的cJSON | |
| cJSON_CreateNull() | 创造对应类型的cJSON |
| cJSON_CreateTrue() | 创造对应类型的cJSON |
| cJSON_CreateFalse() | 创造对应类型的cJSON |
| cJSON_CreateBool() | 创造对应类型的cJSON |
| cJSON_CreateNumber() | 创造对应类型的cJSON |
| cJSON_CreateString() | 创造对应类型的cJSON |
| cJSON_CreateRaw() | 创造对应类型的cJSON |
| cJSON_CreateArray() | 创造对应类型的cJSON |
| cJSON_CreateObject() | 创造对应类型的cJSON |
| 批量创造对应类型的cJSON | |
| cJSON_CreateIntArray() | 批量创造对应类型的cJSON |
| cJSON_CreateFloatArray() | 批量创造对应类型的cJSON |
| cJSON_CreateDoubleArray() | 批量创造对应类型的cJSON |
| cJSON_CreateStringArray() | 批量创造对应类型的cJSON |
| cJSON_AddItemToArray() | 在指定Array后面增加Item |
| cJSON_AddItemToObject() | 在指定Object后面增加Item |
| cJSON_AddItemToObjectCS() | 在指定Object后面增加const Item |
| cJSON_AddItemReferenceToArray() | 在指定Array后面增加Item引用 |
| cJSON_DetachItemViaPointer() | 通过指针从Array删除Item的引用 |
| cJSON_DetachItemFromArray() | 从Array删除Item的引用 |
| cJSON_DeleteItemFromArray() | 从Array删除Item |
| cJSON_DetachItemFromObject() | 从Object删除Item的引用 |
| cJSON_DetachItemFromObjectCaseSensitive() | 大小写敏感的从Object删除Item的引用 |
| cJSON_DeleteItemFromObject() | 从Object删除Item |
| cJSON_DeleteItemFromObjectCaseSensitive() | 大小写敏感的从Object删除Item |
| cJSON_InsertItemInArray() | 在Array指定位置插入Item |
| cJSON_ReplaceItemViaPointer() | 使用指针替代Item |
| cJSON_ReplaceItemInArray() | 替换Array的Item |
| cJSON_ReplaceItemInObject() | 替换Object的Item |
| cJSON_ReplaceItemInObjectCaseSensitive() | 大小写敏感的替换Object的Item |
| cJSON_Duplicate() | 复制cJSON结构体 |
| cJSON_Compare() | 比较两个cJSON结构体 |
| cJSON_Minify() | 将格式化的字符串压缩 |
| cJSON_AddNullToObject() | 调用cJSON_AddItemToObject和cJSON_CreateNull |
| cJSON_AddTrueToObject() | 调用cJSON_AddItemToObject和cJSON_CreateTrue |
| cJSON_AddFalseToObject() | 调用cJSON_AddItemToObject和cJSON_CreateFalse |
| cJSON_AddBoolToObject() | 调用cJSON_AddItemToObject和cJSON_CreateBool |
| cJSON_AddNumberToObject() | 调用cJSON_AddItemToObject和cJSON_CreateNumber |
| cJSON_AddStringToObject() | 调用cJSON_AddItemToObject和cJSON_CreateString |
| cJSON_AddRawToObject() | 调用cJSON_AddItemToObject和cJSON_CreateRaw |
| cJSON_SetIntValue() | 设置int的值,同时也设置double的值 |
| cJSON_SetNumberValue() | 后台会调用cJSON_SetNumberHelper |
| cJSON_SetNumberHelper() | 设置cJSON的number类型的值 |
| cJSON_malloc() | cJSON的malloc函数,调用malloc函数 |
| cJSON_free() | cJSON的free函数,调用free函数 |

浙公网安备 33010602011771号