在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:rafagafe/tiny-json开源软件地址:https://github.com/rafagafe/tiny-json开源编程语言:C 98.9%开源软件介绍:tiny-jsontiny-json is a versatile and easy to use json parser written in C and suitable for embedded systems. It is fast, robust and portable. It is not only a tokenizer. You can access json data in string format or get primitive values directly as C type variables without any loss of performance. You can access the JSON fields one on one or get their values by their names. This helps you to save a lot of source code lines and development time.
If you need to create JSON strings please visit: https://github.com/rafagafe/json-maker PhilosophyWhen parsing a JSON text string a tree is created by linking json_t structures. Navigating or querying this tree is very easy using the provided API. To maintain reduced memory usage and fast processing the strings are not copied. When you request the value of a JSON element, a reference to the original JSON string is returned. To facilitate the processing of the data the returned strings are null-terminated. This is achieved by setting the null character to JSON control characters such as commas, brackets, braces, and quotation marks. APIThe tiny-json API provides two types. typedef enum {
JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN,
JSON_INTEGER, JSON_REAL, JSON_NULL
} jsonType_t; To parse a JSON string use enum { MAX_FIELDS = 4 };
json_t pool[ MAX_FIELDS ];
char str[] = "{ \"name\": \"peter\", \"age\": 32 }";
json_t const* parent = json_create( str, pool, MAX_FIELDS );
if ( parent == NULL ) return EXIT_FAILURE; To get a field by its name we use json_t const* namefield = json_getProperty( parent, "name" );
if ( namefield == NULL ) return EXIT_FAILURE;
if ( json_getType( namefield ) != JSON_TEXT ) return EXIT_FAILURE; To get the value of a field in string format we use char const* namevalue = json_getValue( namefield );
printf( "%s%s%s", "Name: '", namevalue, "'.\n" ); For primitive fields we can use a specific function to get the fields value directly as a C type, f.i. json_t const* agefield = json_getProperty( parent, "age" );
if ( agefield == NULL ) return EXIT_FAILURE;
if ( json_getType( agefield ) != JSON_INTEGER ) return EXIT_FAILURE;
int64_t agevalue = json_getInteger( agefield );
printf( "%s%lld%s", "Age: '", agevalue, "'.\n" );
char const* agetxt = json_getValue( agefield );
printf( "%s%s%s", "Age: '", agetxt, "'.\n" ); For an example how to use nested JSON objects and arrays please see example-01.c. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论