zoukankan      html  css  js  c++  java
  • C语言:json库使用学习

    Json基础

    一、Json的概念

      Json(Javascript Object Notation)全称为JavaScript对象表示法,是一种轻量级的数据交换格式,采用完全独立于语言的文本格式。JSON 是存储和交换文本信息的语法,类似 XML。JSON 比 XML 更小、更快,更易解析。

    1、Json的特性

    JSON 是纯文本

    JSON 具有“自我描述性”(人类可读)

    JSON 具有层级结构(值中存在值)

    JSON 可通过 JavaScript 进行解析

    JSON 数据可使用 AJAX 进行传输

    2、与XML的对比

    Json与XML很是相似,关于它们的不同,主要例举以下几点:

    没有结束标签

    更短

    读写的速度更快

    能够使用内建的 JavaScript eval() 方法进行解析

    使用数组

    不使用保留字

    二、Json的用途

      大致与XML相同

    三、Json的语法简要说明

    1、Json语法规则

    (1)、数据在名称/值对中

    (2)、数据由逗号分隔

    (3)、花括号保存对象

    (4)、方括号保存数组

    2、JSON 名称/值对与Json值

    JSON 数据的书写格式是:名称/值对。

    示例:"firstName" : "John"

    Json的值可以是数字、字符串、布尔值、数组、对象、空值等等。

    3、Json对象

      Json对象在花括号中书写,一个对象可以包含多个名称/值对(即多个数据)。

    4、Json数组

    JSON 数组在方括号中书写:

    数组可包含多个对象:

    {"employees": [

    { "firstName":"John" , "lastName":"Doe" },

    { "firstName":"Anna" , "lastName":"Smith" },

    { "firstName":"Peter" , "lastName":"Jones" }

    ]}

    在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。

    四、Json的简易使用例子

    1、使用json-c库的函数

     1      json_object *my_string;
     2     
     3      my_string = json_object_new_string("	");
     4      g_print("my_string = %s
    ",json_object_get_string(my_string));
     5      g_print("my_string.to_string()=%s
    ",json_object_to_json_string(my_string));
     6      json_object_put(my_string);//减少引用计数
     7 
     8      my_string = json_object_new_string("\");
     9      g_print("my_string=%s
    ", json_object_get_string(my_string));
    10      g_print("my_string.to_string()=%s
    ", json_object_to_json_string(my_string));
    11      json_object_put(my_string);
    12 
    13      my_string = json_object_new_string("foo");
    14      g_print("my_string=%s
    ", json_object_get_string(my_string));
    15      g_print("my_string.to_string()=%s
    ",json_object_to_json_string(my_string));    

    输出结果:
    my_string =     
    my_string.to_string()=" "
    my_string=
    my_string.to_string()="\"
    my_string=foo
    my_string.to_string()="foo"

    说明:

      //Create a new empty json_object of type json_type_xxx

      //创建一个xxx类型的空json对象,引用计数加一

      extern struct json_object* json_object_new_xxx(xxx s);

      //减少obj的引用计数,当计数为0时就释放掉该对象

      int json_object_put(struct json_object *obj);

      通过json_object_get_string与json_object_to_json_string的输出结果可以比较出前者是提取出字符串值并返回,后者是将字符串以json格式返回。

      这两个函数返回的字符串由json_object管理,即my_string被json_object_put减少引用计数至0后字符串内存被释放

     1      json_object *my_array;
     2      my_array = json_object_new_array();
     3      //添加json类型值到数组中
     4      json_object_array_add(my_array,json_object_new_int(1));
     5      json_object_array_add(my_array,json_object_new_int(2));
     6      json_object_array_add(my_array,json_object_new_int(3));
     7      json_object_array_put_idx(my_array,4,json_object_new_int(5));
     8      g_print("my_array=
    ");
     9      for(i=0;i<json_object_array_length(my_array);i++){
    10          json_object *obj = json_object_array_get_idx(my_array,i);
    11          g_print("	[%d]=%s
    ",i,json_object_to_json_string(obj));
    12      }
    13      g_print("my_array.to_string() = %s
    ",json_object_to_json_string(my_array));    
    输出结果:
      my_array=
        [0]=1
        [1]=2
        [2]=3
        [3]=null
        [4]=5
    my_array.to_string() = [ 1, 2, 3, null, 5 ]

    说明:

    //往值为数组类型的json_object中添加json对象。

    extern int json_object_array_add(struct json_object *obj,
                     struct json_object *val);

    //插入或替换在数组中idx索引下的元素,引用计数不会变化

    extern int json_object_array_put_idx(struct json_object *obj, int idx,
                         struct json_object *val);

    //得到类型为数组的json_object的长度

    extern int json_object_array_length(struct json_object *obj);

    //得到数组中索引为idx的元素

    extern struct json_object* json_object_array_get_idx(struct json_object *obj,
                                 int idx);

     1      json_object *my_object;
     2      my_object = json_object_new_object();
     3      //添加json名称和值到json对象集合中
     4      json_object_object_add(my_object,"abc",json_object_new_int(12));
     5      json_object_object_add(my_object,"foo",json_object_new_string("bar"));
     6      json_object_object_add(my_object,"bool0",json_object_new_boolean(FALSE));
     7      json_object_object_add(my_object,"bool1",json_object_new_boolean(TRUE));
     8      json_object_object_add(my_object,"baz",json_object_new_string("bang"));
     9      //同样的key添加会替换掉
    10      json_object_object_add(my_object,"baz",json_object_new_string("fark"));
    11      json_object_object_del(my_object,"baz");
    12 
    13      g_print("my_object=
    ");
    14      //遍历对象集合
    15      json_object_object_foreach(my_object,key,val){
    16          g_print("	%s    :    %s
    ",key,json_object_to_json_string(val));
    17      }
    18      g_print("my_object.to_string() = %s
    ",json_object_to_json_string(my_object));    
    输出结果:
      my_object=
          abc    :    12
          foo    :    "bar"
          bool0    :    false
          bool1    :    true
      my_object.to_string() = { "abc": 12, "foo": "bar", "bool0": false, "bool1": true }

    说明:

      //添加一个json对象字段到类型为object的obj中去

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

      //删除指定的字段,并且被删除的对象的引用计数会被减一

      extern void json_object_object_del(struct json_object* obj, const char *key);

    2、列出json-c里的一些函数做说明

        extern struct json_object* json_object_from_file(const char *filename);

        说明:将参数中制定的json文件转换为json对象

        extern int json_object_to_file(char *filename, struct json_object *obj);

        说明:将json对象写回到文件,并返回状态

        extern struct json_tokener* json_tokener_new(void);

        说明:

          该函数返回一个json_tolener对象并做相应初始化

    struct json_tokener //这是json_tokener的结构定义,关于json解析的信息都包含于此,例如状态、错误等信息
    {
      char *str;
      struct printbuf *pb;
      int max_depth, depth, is_double, st_pos, char_offset;
      enum json_tokener_error err;
      unsigned int ucs_char;
      char quote_char;
      struct json_tokener_srec *stack;
      int flags;
    };

     

        extern void json_tokener_free(struct json_tokener *tok);

        说明:   对tok进行析构,即做一些释放、赋初值等操作。

        extern struct json_object* json_tokener_parse(const char *str);

        说明:  解析一个字符串并返回一个相应类型的json_object对象,例如传入的字符串为"["abc",null,"def",12]",那么便返回一个数组类型的json对象

        extern json_bool json_object_object_get_ex(struct json_object* obj,
                              const char *key,
                                                      struct json_object **value);

        说明:   

          通过参数中给定的字段得到相应的json对象,如果找到就返回true,否则返回false,找到的json对象通过value得到该函数与json_object_object_get功能相同,只是json_object_object_get是直接返回json对象。

       

      

  • 相关阅读:
    元组转换列表
    python切片
    序列类型的方法 增删改查
    python基础 四则运算和数据类型
    linux 常用基础命令操作
    MySQL 命令操作
    linux中如何修改root密码、设置固定IP、安装vmware tools
    虚拟机中网络桥接模式设置
    PHP基础
    HTML基本标签介绍
  • 原文地址:https://www.cnblogs.com/xishuichangliu/p/6214249.html
Copyright © 2011-2022 走看看