0、JSON简介

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。

JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。

这些特性使JSON成为理想的数据交换语言。 跟XML相比,JSON的优势在于格式简洁短小,特别是在处理大量复杂数据的时候,这个优势便显得非常突出。

从各浏览器的支持来看,JSON解决了因不同浏览器对XML DOM解析方式不同而引起的问题。

★JASON的学习资料

http://www.json.org/ (英文)   http://www.json.org/json-zh.html (中文)

http://www.w3school.com.cn/json/

http://www.ibm.com/developerworks/cn/web/wa-lo-json/

json标准 https://tools.ietf.org/html/rfc7159

★JASON-C的简介

JSON库多种多样,但是JSON-C由于兼容性好,支持UTF-8,所以使用比较广泛。

就json来说,由于结构比较简单,不用库也是可以的。

但json-c提供了超出json范围的一些功能,实际上完成了数据序列化和反序列化,数据的存储和检索,数据对象化等功能。还是非常有使用价值的。

https://github.com/json-c/json-c/wiki

官方API手册 : http://json-c.github.io/json-c/json-c-0.13.1/doc/html/index.html

http://zengriguang.blog.163.com/blog/static/17076248720121080187635/

 

1、下载安装JSON-C

1.获取源码
[root]# yum install git
[root]# git clone https://github.com/json-c/json-c.git

2.编译安装
[root]# cd json-c
[root]# ./autogen.sh
[root]# ./configure
[root]# make
[root]# make install

3.查看是否存在库文件
修改ldconfig配置文件,将路径添加进去
vi /etc/ld.so.conf.d/fakeroot-x86_64-linux-gnu.conf
/usr/local/lib       #如果重名,这个需要放在前面,否则不会生效
/usr/lib/x86_64-linux-gnu/libfakeroot

[root]# ldconfig -p|grep json-c
        libjson-c.so.4 (libc6,x86-64) => /usr/local/lib/libjson-c.so.4
        libjson-c.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libjson-c.so.2
        libjson-c.so (libc6,x86-64) => /usr/local/lib/libjson-c.so

说明安装成功

2、示例

json-c_demo.c

#include <stdio.h>
#include <json.h>
 
void test_jsonc()
{
 
    struct json_object *infor_object = NULL;
    infor_object = json_object_new_object();
    if (NULL == infor_object)
    {
        printf("new json object failed.\n");
        return;
    }
 
    struct json_object *para_object = NULL;
    para_object = json_object_new_object();
    if (NULL == para_object)
    {
        json_object_put(infor_object);//free
        printf("new json object failed.\n");
        return;
    }
 
    struct json_object *array_object = NULL;
    array_object = json_object_new_array();
    if (NULL == array_object)
    {
        json_object_put(infor_object);//free
        json_object_put(para_object);//free
        printf("new json object failed.\n");
        return;
    }
 
    /*添加json值类型到数组中*/
    json_object_array_add(array_object, json_object_new_int(256));
    json_object_array_add(array_object, json_object_new_int(257));
    json_object_array_add(array_object, json_object_new_int(258));
 
    json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000"));
    json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF"));
    json_object_object_add(para_object, "Visible", json_object_new_int(1));
 
    /*添加json名称和值到json对象集合中*/
    json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo"));
    json_object_object_add(infor_object, "param", para_object);
    json_object_object_add(infor_object, "id", json_object_new_string("101"));
 
    /*添加数组集合到json对象中*/
    json_object_object_add(infor_object, "array", array_object);
 
    printf("-----------json infor ---------------------------\n");
    printf("%s\n", json_object_to_json_string(infor_object));
    printf("-----------json infor ---------------------------\n");
 
    struct json_object *result_object = NULL;
 
 
    result_object =  json_object_object_get(infor_object, "method");
    printf("-----------result_object method ---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object));
    printf("-----------result_object method---------------------------\n");
 
    result_object =  json_object_object_get(infor_object, "param");
    printf("-----------result_object param ---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object));
    printf("-----------result_object param---------------------------\n");
 
    result_object =  json_object_object_get(infor_object, "array");
    printf("-----------result_object  array---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object));
    printf("-----------result_object array---------------------------\n");
 
    int i;
    for(i = 0; i < json_object_array_length(result_object); i++) {
      struct json_object *obj = json_object_array_get_idx(result_object, i);
      printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
    }
 
    json_object_put(infor_object);//free
 
}

int main(int argc, char *argv[])
{
    test_jsonc();
 
    return 0;
}

编译:
1)指定运行路径 -Wl,-rpath=/usr/local/lib/
gcc -o json-c_demo json-c_demo.c  -L/usr/local/lib/ -ljson-c -I/usr/local/include/json-c -Wl,-rpath=/usr/local/lib/

2)将json-c库放在ldconfig环境变量里(见步骤3)
3)设置一个全局变量
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

 

运行结果:

-----------json infor ---------------------------
{ "method": "GetSystemInfo", "param": { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }, "id": "101", "array": [ 256, 257, 258 ] }
-----------json infor ---------------------------
-----------result_object method ---------------------------
"GetSystemInfo"
-----------result_object method---------------------------
-----------result_object param ---------------------------
{ "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }
-----------result_object param---------------------------
-----------result_object  array---------------------------
[ 256, 257, 258 ]
-----------result_object array---------------------------
        [0]=256
        [1]=257
        [2]=258

 

Logo

更多推荐