在 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); // 修改为 falsechar* 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
。如果有更多问题,欢迎继续讨论!