JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人的阅读、编写,同时也便于机器的解析、生成。它是基于 JavaScript Programming Language , Standard ECMA-262 3rd Edition - December 1999 的一个子集,采用完全独立于程序语言的文本格式,也使用了类C语言的习惯(包括C, C , C#, Java, JavaScript, Perl, Python等),这些特性使JSON成为理想的数据交换语言
JSON基于两种结构:
- “名称/值”对的集合(A collection of name/value pairs)。不同的编程语言中,它被理解为对象(object)、记录(record)、结构(struct)、字典(dictionary)、哈希表(hash table)、有键列表(keyed list),或者关联数组 (associative array)
- 值的有序列表(An ordered list of values)。在大部分语言中,它被实现为数组(array),矢量(vector),列表(list),序列(sequence)
这些都是常见的数据结构。绝大部分编程语言都以某种形式支持它们。这使得在各种编程语言之间交换同样格式的数据成为可能
JSON具有以下这些形式(可以在任意标记之间添加空白):
- 对象(object) 是一个无序的“‘名称/值’对”集合。一个对象以“{”开始,“}”结束。每个“名称”后跟一个“:”;“‘名称/值’ 对”之间使用“,”分隔
- 数组(array) 是值(value)的有序集合。一个数组以“[”开始,“]”结束。值之间使用“,”分隔
- 值(value) 可以是""括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套
- 字符串(string) 是由""包围的任意数量Unicode字符的集合,使用转义。一个字符(character)即一个单独的字符串(character string)。JSON的字符串(string)与C或者Java的字符串非常相似
- 数值(number) 也与C或者Java的数值非常相似。只是JSON的数值没有使用八进制与十六进制格式
以上内容转自"JSON中文网"
http://www.json.org.cn/index.htm
Jansson是一个C库,用于编码、解码和操作JSON数据。其主要特点和设计原则是:
- 简单直观的API和数据模型
- 详尽的文档
- 不依赖其他库
- 支持UTF-8
- 全面的测试集
其版本如下
Version | Date |
---|---|
2.14 | 2021-09-09 |
2.13.1 | 2020-05-07 |
2.13 | 2020-05-05 |
2.12 | 2018-11-26 |
2.11 | 2018-02-09 |
2.10 | 2017-03-02 |
2.9 | 2016-09-18 |
2.8 | 2016-08-30 |
2.7 | 2014-10-02 |
2.6 | 2014-02-11 |
2.5 | 2013-09-19 |
2.4 | 2012-09-23 |
2.3.1 | 2012-04-20 |
2.3 | 2012-01-27 |
2.2.1 | 2011-10-06 |
2.2 | 2011-09-03 |
2.1 | 2011-06-10 |
2.0.1 | 2011-03-31 |
2.0 | 2011-02-28 |
1.3 | 2010-06-13 |
1.2.1 | 2010-04-03 |
1.2 | 2010-01-21 |
1.1.3 | 2009-12-18 |
1.1.2 | 2009-11-08 |
1.1.1 | 2009-10-26 |
1.1 | 2009-10-20 |
1.0.4 | 2009-10-11 |
1.0.3 | 2009-09-14 |
1.0.2 | 2009-09-08 |
1.0.1 | 2009-09-04 |
1.0 | 2009-08-25 |
以上内容转自Jansson官网
https://github.com/akheron/jansson
VxWorks7大概从SR0470开始支持Jansson,目前主要是基于2.12移植的2.12.1
要想在Vx7里使用Jansson,首先在VSB中包含JANSSON,然后在VIP中包含INCLUDE_JSON
Jansson的主要API有
代码语言:javascript复制#define JSON_ERROR_TEXT_LENGTH 160
#define JSON_ERROR_SOURCE_LENGTH 80
typedef struct json_error_t
{
int line;
int column;
int position;
char source[JSON_ERROR_SOURCE_LENGTH];
char text[JSON_ERROR_TEXT_LENGTH];
}json_error_t;
/*
flags
default value is 0
JSON_REJECT_DUPLICATES
Issue a decoding error if any JSON object in the input text contains duplicate keys
JSON_DECODE_ANY
By default, the decoder expects an array or object as the input.
With this flag enabled, the decoder accepts any valid JSON value.
JSON_DISABLE_EOF_CHECK
By default, the decoder expects that its whole input constitutes a valid JSON text, and issues an error if there's extra data after the otherwise valid JSON input.
With this flag enabled, the decoder stops after decoding a valid JSON array or object, and thus allows extra data after the JSON text.
Normally, reading will stop when the last ] or } in the JSON input is encountered.
If both JSON_DISABLE_EOF_CHECK and JSON_DECODE_ANY flags are used, the decoder may read one extra UTF-8 code unit (up to 4 bytes of input).
For example, decoding 4true correctly decodes the integer 4, but also reads the t.
For this reason, if reading multiple consecutive values that are not arrays or objects, they should be separated by at least one whitespace character.
JSON_DECODE_INT_AS_REAL
JSON defines only one number type.
Jansson distinguishes between ints and reals.
With this flag enabled the decoder interprets all numbers as real values.
Integers that do not have an exact double representation will silently result in a loss of precision.
Integers that cause a double overflow will cause an error.
JSON_ALLOW_NUL
Allow u0000 escape inside string values.
This is a safety measure; If you know your input can contain null bytes, use this flag.
If you don’t use this flag, you don’t have to worry about null bytes inside strings unless you explicitly create themselves by using e.g. json_stringn() or s# format specifier for json_pack().
Object keys cannot have embedded null bytes even if this flag is used.
*/
/*
* Decodes the JSON text in file path and
* returns the array or object it contains
*/
json_t *json_load_file
(
const char *path,
size_t flags,
json_error_t *error
);
/*
* Decodes the JSON string input and
* returns the array or object it contains
*/
json_t *json_loads
(
const char *input,
size_t flags,
json_error_t *error
);
/* Returns the number of elements in object */
size_t json_object_size(const json_t *object);
/*
* Returns the JSON representation of json as a string
* The return value must be freed by the caller using free()
*/
char *json_dumps(const json_t *json, size_t flags);
/* Get a value corresponding to key from object */
json_t *json_object_get(const json_t *object, const char *key);
/* Returns the associated value of string as a null terminated UTF-8 encoded string */
const char *json_string_value(const json_t *string);
/* Returns an opaque iterator which can be used to iterate over all key-value pairs in object */
void *json_object_iter(json_t *object);
/* Like json_object_iter(), but returns an iterator to the key-value pair in object whose key is equal to key */
void *json_object_iter_at(json_t *object, const char *key);
/* Returns an iterator pointing to the next key-value pair in object after iter */
void *json_object_iter_next(json_t *object, void *iter);
/* Extract the associated key from iter */
const char *json_object_iter_key(void *iter);
/* Extract the associated value from iter */
json_t *json_object_iter_value(void *iter);
写个例子
代码语言:javascript复制/*
* 版权所有 公众号 VxWorks567
* Vx7
*/
#include <string.h>
#include <stdio.h>
#include "jansson.h"
void testJsonGet(char *jsonfile)
{
int i;
int count;
char *result;
void *iter;
json_t *data;
json_error_t error;
#if 0
if(jsonfile == NULL)
jsonfile = "json.txt";
data = json_load_file(jsonfile, 0, &error);
#else
data = json_loads("{"name1": "value1", "name2": "value2", "name3": "value3", "name4": "value4"}", 0, &error);
#endif
if(data == NULL)
{
printf("errno: line %d, %sn", error.line, error.text);
return;
}
count = json_object_size(data);
/* 取出object的数据 */
result = json_dumps(data, 0);
printf("Json data count: %dn", count);
printf("%sn", result);
free(result);
/* 打印某个key的value */
printf("### %sn", json_string_value(json_object_get(data, "name1")));
/* 打印所有的key和value */
iter = json_object_iter(data);
for(i=0; iter!=NULL; i )
{
printf("%d: keyword = %s, value = %sn", i,
json_object_iter_key(iter),
json_string_value(json_object_iter_value(iter)));
iter = json_object_iter_next(data, iter);
}
return;
}
这个Jansson库应该可以很容易地移植到低版本的VxWorks...