当前位置: 首页 > 工具软件 > parson > 使用案例 >

c语言JSON库 Parson的编译和使用

柏正平
2023-12-01

c语言JSON库 Parson的编译和使用

最近一直学习ndk,Android肯定会用到json,这里介绍一个c语言写成的JSON解析库

Parson介绍

Parson is a lighweight json library written in C.

c语言中使用的一个轻量级的json库

特点:

  • Full JSON support
  • Lightweight (only 2 files)
  • Simple API
  • Addressing json values with dot notation (similar to C structs or objects in most OO languages, e.g. “objectA.objectB.value”)
  • C89 compatible
  • Test suites

地址: https://github.com/kgabis/parson.git

简单使用

  1. 构造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"
            ]
        }
    }
    

代码收录

https://github.com/ddssingsong/AnyNdk

 类似资料: