​json-c简介及基本使用

​json-c简介及基本使用

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JSON选用完全独立于言语的文本格局,但是也使用了类似于C言语宗族的习气(包含C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使json调试成为抱负的数据交换言语。 易于人阅览和编写,同时也易于机器解析和生成(一般用于提高网络传输速率)。

JSON的格式
JSON 数据的书写格式是键(名称)/值对
JSON 键值对是用来保存 JS 对象的一种方式,和 JS 对象的写法也大同小异,键/值对包括字段名称(在双引号中),后面写一个冒号,然后是值。
JSON 值可以是:字符串(在双引号中)、数组(在中括号中)、数字(整数或浮点数)、逻辑值(true 或 false)、对象(在大括号中)、 null。

JSON结构
JSON结构有两种结构,就是对象和数组。通过这两种结构可以表示各种复杂的结构。
{"province": "Shanxi"} 可以理解为是一个包含province为Shanxi的对象,
["Shanxi","Shandong"]这是一个包含两个元素的数组
[{"province": "Shanxi"},{"province": "Shandong"}] 就表示包含两个对象的数组。

当然了,你也可以使用 {"province":["Shanxi","Shandong"]} 来简化上面的JSON,这是一个拥有一个name数组的对象。

下面是一小段JSON代码:

{“skillz”: {“web”:[ {“name”: “html”, “years”: “5” }, {“name”: “css”, “years”: “3” }],”database”:[ {“name”: “sql”, “years”: “7” }]}}

花括弧,方括弧,冒号和逗号
花括弧表示一个“容器”
方括号装载数组
名称和值用冒号隔开

数组元素通过逗号隔开

json-c数据类型

typedef enum json_type {
  /* If you change this, be sure to update json_type_to_name() too */
  json_type_null,
  json_type_boolean,
  json_type_double,
  json_type_int,
  json_type_object,
  json_type_array,
  json_type_string,
} json_type;

基础API

extern struct json_object* json_object_new_object(void);

extern struct json_object* json_object_new_boolean(json_bool b);
extern struct json_object* json_object_new_double(double d);
extern struct json_object* json_object_new_int(int32_t i);
extern struct json_object* json_object_new_int64(int64_t i);
extern struct json_object* json_object_new_array(void);
extern struct json_object* json_object_new_string(const char *s);

json_object_object_add(struct json_object* obj, const char *key,
				   struct json_object *val);

get

extern json_bool json_object_get_boolean(struct json_object *obj);
extern double json_object_get_double(struct json_object *obj);
extern int32_t json_object_get_int(struct json_object *obj);
extern int64_t json_object_get_int64(struct json_object *obj);
extern const char* json_object_get_string(struct json_object *obj);
extern int json_object_array_length(struct json_object *obj);
extern struct array_list* json_object_get_array(struct json_object *obj);

json-c 创建json对象

/* object type methods */

/** Create a new empty object with a reference count of 1.  The caller of
 * this object initially has sole ownership.  Remember, when using
 * json_object_object_add or json_object_array_put_idx, ownership will
 * transfer to the object/array.  Call json_object_get if you want to maintain
 * shared ownership or also add this object as a child of multiple objects or
 * arrays.  Any ownerships you acquired but did not transfer must be released
 * through json_object_put.
 *
 * @returns a json_object of type json_type_object
 */
extern struct json_object* json_object_new_object(void);

    struct json_object *myJson = NULL;
    struct json_object *boolType = NULL;
    struct json_object *intType = NULL;
    struct json_object *string = NULL;
    struct json_object *array = NULL;
    struct json_object *oneInArray = NULL;
    const char* content = NULL;

    myJson = json_object_new_object();
    /*bool*/
    boolType = json_object_new_boolean(false);
    json_object_object_add(myJson, "BoolType", boolType);
	/*int*/
    intType = json_object_new_int(1);
    json_object_object_add(myJson, "IntType", intType);
	/*string*/
    string = json_object_new_string("string");
    json_object_object_add(myJson, "String", string);
    /*array*/
    array = json_object_new_array();
    json_object_object_add(myJson, "Array", array);
    size_t loop = 0;
    for (loop = 0; loop < 2; loop ++)
    {
        /*create one object and add it to array*/
        oneInArray = json_object_new_object();
        json_object_array_add(array, oneInArray);
        /*int*/
        intType = json_object_new_int(2);
        json_object_object_add(oneInArray, "IntTypeInArray", intType);
        /*string*/
        string = json_object_new_string("stringInArray");
        json_object_object_add(oneInArray, "StringInArray", string);
    }

	content = json_object_to_json_string(myJson);
	printf("content %s\n", content);

	/*only need to free main object*/
	json_object_put(myJson);
注意事项:
1. json_object_new_object生成的对象需要释放
/** Create a new empty object with a reference count of 1.  The caller of
 * this object initially has sole ownership.  Remember, when using
 * json_object_object_add or json_object_array_put_idx, ownership will
 * transfer to the object/array.  Call json_object_get if you want to maintain
 * shared ownership or also add this object as a child of multiple objects or
 * arrays.  Any ownerships you acquired but did not transfer must be released
 * through json_object_put.
 *
 * @returns a json_object of type json_type_object
 */
json_object_new_object生成的对象必须调用json_object_put释放(主object)。

2. json_tokener_parse生成的对象需要释放
json_tokener_parse生成的对象,必须使用json_object_put释放.

3. json_object_object_get出来的对象要不要释放
通过json_object_object_get获取的对象不能单独释放,因为它仍然归父节点所有。

4. 通过json_object_object_add添加到其他节点的,能不能释放
通过json_object_object_add添加到其他节点的不能再单独释放,因为他已经成为别人的子节点,他的生命周期由父节点维护了。
    
5. json_object_to_json_string获取到的字串要不要释放
这个free也是非法的,因为json_object_to_json_string只是把json对象内部的指针暴露给你了,借你用下而已,千万别释放。

json-c 解析json对象

int
JsonSafeGetI32(
    __in struct json_object *JObj,
    __in const char *Key,
    __out int32_t *ValInt
    )
{
    struct json_object *valueObjPtr = NULL;
    int32_t val = 0;
    int ret = 0;

    if (json_object_object_get_ex(JObj, Key, &valueObjPtr))
    {
        val = json_object_get_int(valueObjPtr);
        switch (val)
        {
            case 0:
                if (EINVAL == errno)  ret = -EINVAL;
                break;
            case INT32_MAX:
            case INT32_MIN:
                ret = -EOVERFLOW;
                break;
            default:
                break;
        }
    }
    else
    {
        ret = -EINVAL;
    }

    *ValInt = val;

    return ret;
}

int
JsonSafeGetBool(
    __in struct json_object *JObj,
    __in const char *Key,
    __out BOOL *ValBool
    )
{
    int ret = 0;
    struct json_object *valueObjPtr = NULL;

    if (json_object_object_get_ex(JObj, Key, &valueObjPtr))
    {
        *ValBool = (BOOL)json_object_get_boolean(valueObjPtr);
    }
    else
    {
        ret = -EINVAL;
    }

    return ret;
}

int
JsonSafeGetStr(
    __in struct json_object *JObj,
    __in const char *Key,
    __out_bcount(OutStrSize) char *OutStr,
    __in size_t OutStrSize
    )
{
    int ret = 0;
    struct json_object *jsonStrPtr = NULL;
    const char *srcStr;
    size_t len;

    json_object_object_get_ex(JObj, Key, &jsonStrPtr);

    /* this function allow input NULL, so don't check JsonStrPtr */
    srcStr = json_object_get_string(jsonStrPtr);
    if (NULL == srcStr)
    {
        ret = -EINVAL;
        goto CommonReturn;
    }

    /*copy your str*/

CommonReturn:
    return ret;
}

parser

	struct json_object *myJson = NULL;
    struct json_object *boolType = NULL;
    struct json_object *intType = NULL;
    struct json_object *string = NULL;
    struct json_object *array = NULL;
    struct json_object *oneInArray = NULL;
	int32_t tmpVal = 0; 
	bool boolType = false;
	size_t arrayNum = 0;
	size_t loop = 0;

	myJson = json_tokener_parse(JsonContentStr);
    //myJson = json_object_from_file("./JsonContent.json");
	/*bool*/
	JsonSafeGetBool(myJson, "BoolType", &boolType);
	/*int*/
    JsonSafeGetI32(myJson, "IntType", &tmpVal);
	/*str*/
	JsonSafeGetStr(myJson, "String", str);
	/*Array*/
	
	json_object_object_get_ex(myJson, "Array", &array);
    arrayNum = json_object_array_length(array);
	for(loop = 0; loop < arrayNum; loop++)
    {
        oneInArray = json_object_array_get_idx(array, loop);
        /*int*/
        JsonSafeGetI32(oneInArray, "IntTypeInArray", &tmpVal);
        /*string*/
        JsonSafeGetStr(oneInArray, "StringInArray", str);
    }
    
    json_object_put(myJson);
  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JSON-C是一个C语言库,用于解析、生成和操作JSON数据。它提供了一组简单的API,使开发人员可以轻松地将JSON数据集成到他们的C语言应用程序中。 以下是一个简单的例子,展示了如何使用JSON-C库解析JSON数据: ```c #include <stdio.h> #include <json-c/json.h> int main() { const char *json_string = "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }"; struct json_object *json_obj = json_tokener_parse(json_string); // 获取JSON对象中的值 struct json_object *name_obj, *age_obj, *city_obj; json_object_object_get_ex(json_obj, "name", &name_obj); json_object_object_get_ex(json_obj, "age", &age_obj); json_object_object_get_ex(json_obj, "city", &city_obj); // 打印JSON值 printf("Name: %s\n", json_object_get_string(name_obj)); printf("Age: %d\n", json_object_get_int(age_obj)); printf("City: %s\n", json_object_get_string(city_obj)); // 释放内存 json_object_put(json_obj); return 0; } ``` 在上面的示例中,我们首先定义了一个JSON字符串,然后使用`json_tokener_parse()`函数将其解析为一个JSON对象。接下来,我们使用`json_object_object_get_ex()`函数获取JSON对象中的各个值。最后,我们使用`json_object_get_string()`和`json_object_get_int()`函数获取字符串和整数值,并将它们打印到控制台上。最后,我们使用`json_object_put()`函数释放JSON对象的内存。 需要注意的是,在使用JSON-C库时,需要在编译时链接`-ljson-c`库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值