最近一直学习ndk,Android肯定会用到json,这里介绍一个c语言写成的JSON解析库
Parson is a lighweight json library written in C.
c语言中使用的一个轻量级的json库
特点:
地址: https://github.com/kgabis/parson.git
构造json
void serialization_example(void) {
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "name", "John Smith");
json_object_set_number(root_object, "age", 25);
json_object_dotset_string(root_object, "address.city", "Cupertino");
json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"email@example.com\",\"email2@example.com\"]"));
serialized_string = json_serialize_to_string_pretty(root_value);
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}
输出内容:
{
"name": "John Smith",
"age": 25,
"address": {
"city": "Cupertino"
},
"contact": {
"emails": [
"email@example.com",
"email2@example.com"
]
}
}