Linux json-c 安装和使用

json-c安装和使用

  • json-c
    • 一. 安装 json-c
      • 1. 命令安装
      • 2. 下载源码安装
    • 二. 使用 json-c
      • 1. 测试是否安装成功
      • 2. json-c 基本介绍
        • 1. json_type
        • 2. json_object_new_object()
        • 3. json_object_object_add()
        • 4. json_object_object_get_ex()
        • 5. json_object_object_length()
        • 6. json_object_to_json_string()
        • 7. json_object_to_json_string_ext()
        • 8. json_object_to_json_string_length()
        • 9. json_object_is_type()
        • 10. json_object_get_type()
        • 11. json_object_get()
        • 12. json_object_new_array()
        • 13. json_object_array_add()
        • 14. json_object_array_length()
    • 三. 编写 json-c 示例

json-c

一. 安装 json-c

1. 命令安装

    1. sudo apt-get update (非必须)
    1. sudo apt-get install libjson-c-dev

2. 下载源码安装

    1. 打开网址链接
    1. 下载源码
    • 方式1: 直接下载zip
      Linux json-c 安装和使用_第1张图片

    • 方式2: 用 git 命令下载

      • git clone https://github.com/json-c/json-c.git

        1. 创建 build目录
          • mkdir build
        1. 进入build 目录
          • cd build/
        1. 执行 cmake 生成 Makefile
          • cmake ..
          • 没有 cmake 可以 执行 sudo apt install cmake 安装 cmake
          • 执行过程如下
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for fcntl.h
-- Looking for fcntl.h - found
-- Looking for inttypes.h
-- Looking for inttypes.h - found
-- Looking for stdarg.h
-- Looking for stdarg.h - found
-- Looking for strings.h
-- Looking for strings.h - found
-- Looking for string.h
-- Looking for string.h - found
-- Looking for syslog.h
-- Looking for syslog.h - found
-- Looking for 4 include files stdlib.h, ..., float.h
-- Looking for 4 include files stdlib.h, ..., float.h - found
-- Looking for unistd.h
-- Looking for unistd.h - found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for sys/resource.h
-- Looking for sys/resource.h - found
-- Looking for dlfcn.h
-- Looking for dlfcn.h - found
-- Looking for endian.h
-- Looking for endian.h - found
-- Looking for limits.h
-- Looking for limits.h - found
-- Looking for locale.h
-- Looking for locale.h - found
-- Looking for memory.h
-- Looking for memory.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stdlib.h
-- Looking for stdlib.h - found
-- Looking for sys/cdefs.h
-- Looking for sys/cdefs.h - found
-- Looking for sys/param.h
-- Looking for sys/param.h - found
-- Looking for sys/random.h
-- Looking for sys/random.h - found
-- Looking for sys/stat.h
-- Looking for sys/stat.h - found
-- Looking for xlocale.h
-- Looking for xlocale.h - not found
-- Looking for _isnan
-- Looking for _isnan - not found
-- Looking for _finite
-- Looking for _finite - not found
-- Looking for INFINITY
-- Looking for INFINITY - found
-- Looking for isinf
-- Looking for isinf - found
-- Looking for isnan
-- Looking for isnan - found
-- Looking for nan
-- Looking for nan - found
-- Looking for _doprnt
-- Looking for _doprnt - not found
-- Looking for snprintf
-- Looking for snprintf - found
-- Looking for vasprintf
-- Looking for vasprintf - found
-- Looking for vsnprintf
-- Looking for vsnprintf - found
-- Looking for vprintf
-- Looking for vprintf - found
-- Looking for arc4random
-- Looking for arc4random - not found
-- Looking for bsd/stdlib.h
-- Looking for bsd/stdlib.h - not found
-- Looking for open
-- Looking for open - found
-- Looking for realloc
-- Looking for realloc - found
-- Looking for setlocale
-- Looking for setlocale - found
-- Looking for uselocale
-- Looking for uselocale - found
-- Looking for strcasecmp
-- Looking for strcasecmp - found
-- Looking for strncasecmp
-- Looking for strncasecmp - found
-- Looking for strdup
-- Looking for strdup - found
-- Looking for strerror
-- Looking for strerror - found
-- Looking for vsyslog
-- Looking for vsyslog - found
-- Looking for getrandom
-- Looking for getrandom - found
-- Looking for getrusage
-- Looking for getrusage - found
-- Looking for strtoll
-- Looking for strtoll - found
-- Looking for strtoull
-- Looking for strtoull - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of int
-- Check size of int - done
-- Check size of int64_t
-- Check size of int64_t - done
-- Check size of long
-- Check size of long - done
-- Check size of long long
-- Check size of long long - done
-- Check size of size_t
-- Check size of size_t - done
-- Check size of ssize_t
-- Check size of ssize_t - done
-- Performing Test HAS_GNU_WARNING_LONG
-- Performing Test HAS_GNU_WARNING_LONG - Failed
-- Performing Test HAVE_ATOMIC_BUILTINS
-- Performing Test HAVE_ATOMIC_BUILTINS - Success
-- Performing Test HAVE___THREAD
-- Performing Test HAVE___THREAD - Success
-- Wrote /home/pikaqiu/json-c/json-c_source_code/json-c/build/config.h
-- Wrote /home/pikaqiu/json-c/json-c_source_code/json-c/build/json_config.h
-- Performing Test REENTRANT_WORKS
-- Performing Test REENTRANT_WORKS - Success
-- Performing Test BSYMBOLIC_WORKS
-- Performing Test BSYMBOLIC_WORKS - Success
-- Performing Test VERSION_SCRIPT_WORKS
-- Performing Test VERSION_SCRIPT_WORKS - Success
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
Warning: doxygen not found, the 'doc' target will not be included
-- Wrote /home/pikaqiu/json-c/json-c_source_code/json-c/build/apps_config.h
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pikaqiu/json-c/json-c_source_code/json-c/build
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$
    1. 可以 ls 查看是否有 Makefile 我这有了。
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ ls
apps                      CMakeFiles           CPackSourceConfig.cmake  json-c-config.cmake  Makefile
apps_config.h             cmake_install.cmake  CTestTestfile.cmake      json_config.h        Testing
check-version-script.sym  config.h             DartConfiguration.tcl    json-c.pc            tests
CMakeCache.txt            CPackConfig.cmake    doc                      json.h
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$
    1. 执行 make 编译
    • make
    • 执行过程如下
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ make
Scanning dependencies of target json-c
[  1%] Building C object CMakeFiles/json-c.dir/arraylist.c.o
[  2%] Building C object CMakeFiles/json-c.dir/debug.c.o
[  3%] Building C object CMakeFiles/json-c.dir/json_c_version.c.o
[  4%] Building C object CMakeFiles/json-c.dir/json_object.c.o
[  5%] Building C object CMakeFiles/json-c.dir/json_object_iterator.c.o
[  7%] Building C object CMakeFiles/json-c.dir/json_tokener.c.o
[  8%] Building C object CMakeFiles/json-c.dir/json_util.c.o
[  9%] Building C object CMakeFiles/json-c.dir/json_visit.c.o
[ 10%] Building C object CMakeFiles/json-c.dir/linkhash.c.o
[ 11%] Building C object CMakeFiles/json-c.dir/printbuf.c.o
[ 13%] Building C object CMakeFiles/json-c.dir/random_seed.c.o
[ 14%] Building C object CMakeFiles/json-c.dir/strerror_override.c.o
[ 15%] Building C object CMakeFiles/json-c.dir/json_pointer.c.o
[ 16%] Linking C shared library libjson-c.so
[ 16%] Built target json-c
Scanning dependencies of target json-c-static
[ 17%] Building C object CMakeFiles/json-c-static.dir/arraylist.c.o
[ 19%] Building C object CMakeFiles/json-c-static.dir/debug.c.o
[ 20%] Building C object CMakeFiles/json-c-static.dir/json_c_version.c.o
[ 21%] Building C object CMakeFiles/json-c-static.dir/json_object.c.o
[ 22%] Building C object CMakeFiles/json-c-static.dir/json_object_iterator.c.o
[ 23%] Building C object CMakeFiles/json-c-static.dir/json_tokener.c.o
[ 25%] Building C object CMakeFiles/json-c-static.dir/json_util.c.o
[ 26%] Building C object CMakeFiles/json-c-static.dir/json_visit.c.o
[ 27%] Building C object CMakeFiles/json-c-static.dir/linkhash.c.o
[ 28%] Building C object CMakeFiles/json-c-static.dir/printbuf.c.o
[ 29%] Building C object CMakeFiles/json-c-static.dir/random_seed.c.o
[ 30%] Building C object CMakeFiles/json-c-static.dir/strerror_override.c.o
[ 32%] Building C object CMakeFiles/json-c-static.dir/json_pointer.c.o
[ 33%] Linking C static library libjson-c.a
[ 33%] Built target json-c-static
Scanning dependencies of target test_float
[ 34%] Building C object tests/CMakeFiles/test_float.dir/test_float.c.o
[ 35%] Linking C executable test_float
[ 35%] Built target test_float
Scanning dependencies of target test_parse
[ 36%] Building C object tests/CMakeFiles/test_parse.dir/test_parse.c.o
[ 38%] Linking C executable test_parse
[ 38%] Built target test_parse
Scanning dependencies of target test_deep_copy
[ 39%] Building C object tests/CMakeFiles/test_deep_copy.dir/test_deep_copy.c.o
[ 40%] Linking C executable test_deep_copy
[ 40%] Built target test_deep_copy
Scanning dependencies of target test_charcase
[ 41%] Building C object tests/CMakeFiles/test_charcase.dir/test_charcase.c.o
[ 42%] Linking C executable test_charcase
[ 42%] Built target test_charcase
Scanning dependencies of target test_cast
[ 44%] Building C object tests/CMakeFiles/test_cast.dir/test_cast.c.o
[ 45%] Linking C executable test_cast
[ 45%] Built target test_cast
Scanning dependencies of target test_compare
[ 46%] Building C object tests/CMakeFiles/test_compare.dir/test_compare.c.o
[ 47%] Linking C executable test_compare
[ 47%] Built target test_compare
Scanning dependencies of target test_int_add
[ 48%] Building C object tests/CMakeFiles/test_int_add.dir/test_int_add.c.o
[ 50%] Linking C executable test_int_add
[ 50%] Built target test_int_add
Scanning dependencies of target test2Formatted
[ 51%] Building C object tests/CMakeFiles/test2Formatted.dir/test2.c.o
[ 52%] Building C object tests/CMakeFiles/test2Formatted.dir/parse_flags.c.o
[ 53%] Linking C executable test2Formatted
[ 53%] Built target test2Formatted
Scanning dependencies of target testReplaceExisting
[ 54%] Building C object tests/CMakeFiles/testReplaceExisting.dir/testReplaceExisting.c.o
[ 55%] Linking C executable testReplaceExisting
[ 55%] Built target testReplaceExisting
Scanning dependencies of target test1
[ 57%] Building C object tests/CMakeFiles/test1.dir/test1.c.o
[ 58%] Linking C executable test1
[ 58%] Built target test1
Scanning dependencies of target test4
[ 59%] Building C object tests/CMakeFiles/test4.dir/test4.c.o
[ 60%] Linking C executable test4
[ 60%] Built target test4
Scanning dependencies of target test2
[ 61%] Building C object tests/CMakeFiles/test2.dir/test2.c.o
[ 63%] Linking C executable test2
[ 63%] Built target test2
Scanning dependencies of target test_visit
[ 64%] Building C object tests/CMakeFiles/test_visit.dir/test_visit.c.o
[ 65%] Linking C executable test_visit
[ 65%] Built target test_visit
Scanning dependencies of target test_double_serializer
[ 66%] Building C object tests/CMakeFiles/test_double_serializer.dir/test_double_serializer.c.o
[ 67%] Linking C executable test_double_serializer
[ 67%] Built target test_double_serializer
Scanning dependencies of target test_set_serializer
[ 69%] Building C object tests/CMakeFiles/test_set_serializer.dir/test_set_serializer.c.o
[ 70%] Linking C executable test_set_serializer
[ 70%] Built target test_set_serializer
Scanning dependencies of target test_json_pointer
[ 71%] Building C object tests/CMakeFiles/test_json_pointer.dir/test_json_pointer.c.o
[ 72%] Linking C executable test_json_pointer
[ 72%] Built target test_json_pointer
Scanning dependencies of target test_null
[ 73%] Building C object tests/CMakeFiles/test_null.dir/test_null.c.o
[ 75%] Linking C executable test_null
[ 75%] Built target test_null
Scanning dependencies of target test_strerror
[ 76%] Building C object tests/CMakeFiles/test_strerror.dir/test_strerror.c.o
[ 77%] Building C object tests/CMakeFiles/test_strerror.dir/__/strerror_override.c.o
[ 78%] Linking C executable test_strerror
[ 78%] Built target test_strerror
Scanning dependencies of target test_printbuf
[ 79%] Building C object tests/CMakeFiles/test_printbuf.dir/test_printbuf.c.o
[ 80%] Linking C executable test_printbuf
[ 80%] Built target test_printbuf
Scanning dependencies of target test_set_value
[ 82%] Building C object tests/CMakeFiles/test_set_value.dir/test_set_value.c.o
[ 83%] Linking C executable test_set_value
[ 83%] Built target test_set_value
Scanning dependencies of target test1Formatted
[ 84%] Building C object tests/CMakeFiles/test1Formatted.dir/test1.c.o
[ 85%] Building C object tests/CMakeFiles/test1Formatted.dir/parse_flags.c.o
[ 86%] Linking C executable test1Formatted
[ 86%] Built target test1Formatted
Scanning dependencies of target test_locale
[ 88%] Building C object tests/CMakeFiles/test_locale.dir/test_locale.c.o
[ 89%] Linking C executable test_locale
[ 89%] Built target test_locale
Scanning dependencies of target test_parse_int64
[ 90%] Building C object tests/CMakeFiles/test_parse_int64.dir/test_parse_int64.c.o
[ 91%] Linking C executable test_parse_int64
[ 91%] Built target test_parse_int64
Scanning dependencies of target test_util_file
[ 92%] Building C object tests/CMakeFiles/test_util_file.dir/test_util_file.c.o
[ 94%] Building C object tests/CMakeFiles/test_util_file.dir/__/strerror_override.c.o
[ 95%] Linking C executable test_util_file
[ 95%] Built target test_util_file
Scanning dependencies of target test_object_iterator
[ 96%] Building C object tests/CMakeFiles/test_object_iterator.dir/test_object_iterator.c.o
[ 97%] Linking C executable test_object_iterator
[ 97%] Built target test_object_iterator
Scanning dependencies of target json_parse
[ 98%] Building C object apps/CMakeFiles/json_parse.dir/json_parse.c.o
[100%] Linking C executable json_parse
[100%] Built target json_parse
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ 

    1. 执行 make install 安装
    • sudo make install
    • 执行过程如下
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ sudo make install
[ 16%] Built target json-c
[ 33%] Built target json-c-static
[ 35%] Built target test_float
[ 38%] Built target test_parse
[ 40%] Built target test_deep_copy
[ 42%] Built target test_charcase
[ 45%] Built target test_cast
[ 47%] Built target test_compare
[ 50%] Built target test_int_add
[ 53%] Built target test2Formatted
[ 55%] Built target testReplaceExisting
[ 58%] Built target test1
[ 60%] Built target test4
[ 63%] Built target test2
[ 65%] Built target test_visit
[ 67%] Built target test_double_serializer
[ 70%] Built target test_set_serializer
[ 72%] Built target test_json_pointer
[ 75%] Built target test_null
[ 78%] Built target test_strerror
[ 80%] Built target test_printbuf
[ 83%] Built target test_set_value
[ 86%] Built target test1Formatted
[ 89%] Built target test_locale
[ 91%] Built target test_parse_int64
[ 95%] Built target test_util_file
[ 97%] Built target test_object_iterator
[100%] Built target json_parse
Install the project...
-- Install configuration: "debug"
-- Installing: /usr/local/lib/libjson-c.so.5.2.0
-- Installing: /usr/local/lib/libjson-c.so.5
-- Installing: /usr/local/lib/libjson-c.so
-- Installing: /usr/local/lib/libjson-c.a
-- Installing: /usr/local/lib/cmake/json-c/json-c-targets.cmake
-- Installing: /usr/local/lib/cmake/json-c/json-c-targets-debug.cmake
-- Installing: /usr/local/lib/cmake/json-c/json-c-config.cmake
-- Installing: /usr/local/lib/pkgconfig/json-c.pc
-- Installing: /usr/local/include/json-c/json_config.h
-- Installing: /usr/local/include/json-c/json.h
-- Installing: /usr/local/include/json-c/arraylist.h
-- Installing: /usr/local/include/json-c/debug.h
-- Installing: /usr/local/include/json-c/json_c_version.h
-- Installing: /usr/local/include/json-c/json_inttypes.h
-- Installing: /usr/local/include/json-c/json_object.h
-- Installing: /usr/local/include/json-c/json_object_iterator.h
-- Installing: /usr/local/include/json-c/json_tokener.h
-- Installing: /usr/local/include/json-c/json_types.h
-- Installing: /usr/local/include/json-c/json_util.h
-- Installing: /usr/local/include/json-c/json_visit.h
-- Installing: /usr/local/include/json-c/linkhash.h
-- Installing: /usr/local/include/json-c/printbuf.h
-- Installing: /usr/local/include/json-c/json_pointer.h
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c/build$ 

这样就完成了~

二. 使用 json-c

1. 测试是否安装成功

我这边是 下载源码方式安装的。

  • 编写一个程序 检验是否真的安装完成。

    #include 
    
    int main(int argc, char **argv)
    {
    	struct json_object *obj = json_object_new_object();
    	return 0;
    }
    
  • 2.运行

    • 运行程序报错
    • 报错信息如下
pikaqiu@ubuntu:~/linux_c/demo/play_music$ ./a.out 
./a.out: error while loading shared libraries: libjson-c.so.5: cannot open shared object file: No such file or directory
pikaqiu@ubuntu:~/linux_c/demo/play_music$
    1. find 寻找是否有该文件
    • find /usr/local/lib/ -name libjson-c.so.5

    • 信息如下,确认存在

pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c$ find /usr/local/lib/ -name libjson-c.so.5
/usr/local/lib/libjson-c.so.5
pikaqiu@ubuntu:~/json-c/json-c_source_code/json-c$ 
    1. 查看文件信息
    • ls -l /usr/local/lib/libjson-c.so.5
    • 发现是软链接
pikaqiu@ubuntu:~/linux_c/demo/play_music$ ls -l /usr/local/lib/libjson-c.so.5
lrwxrwxrwx 1 root root 18 Aug 26 23:26 /usr/local/lib/libjson-c.so.5 -> libjson-c.so.5.2.0
pikaqiu@ubuntu:~/linux_c/demo/play_music$ 
    1. 添加到 /usr/lib/
    • 操作如下
pikaqiu@ubuntu:~/linux_c/demo/play_music$ sudo ln -s /usr/local/lib/libjson-c.so.5.2.0 /usr/lib/libjson-c.so.5
[sudo] password for pikaqiu: 
pikaqiu@ubuntu:~/linux_c/demo/play_music$ 

问题解决。

2. json-c 基本介绍

这里只是写一些可能常用的。具体的可以看源码。

1. json_type

  • json_object.h
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;

2. json_object_new_object()

  • 描述: 创建一个 json 对象

  • 原型: struct json_object* json_object_new_object(void);

  • 参数: 无

  • 返回: json对象

  • 示例:

/* 创建一个 json 对象 */
struct json_object *obj = json_object_new_object();

3. json_object_object_add()

  • 描述: 给 json 对象添加键值。

  • 原型: int json_object_object_add(struct json_object* obj, const char *key, struct json_object *val);

  • 参数: obj: 要添加的对象,key:键, val:值

  • 返回: 成功: 0 失败: 负值

  • 示例: { "name": "jack" }

/* 创建一个 json 对象 */
struct json_object *obj = json_object_new_object();
/* 给 json 对象 添加内容 */
json_object_object_add(obj, "name", json_object_new_string("jack"));

4. json_object_object_get_ex()

  • 描述: 给 json 对象 进行解析。

  • 原型: json_bool json_object_object_get_ex(const struct json_object *obj, const char *key, struct json_object **value);

  • 参数: obj: 要进行解析的对象,key:键, val:存放 键 对应的值。

  • 返回: 存在: true 不存在: false

  • 示例:

/* 根据键名 解析出 json 对象 */
struct json_object *json;
json_object_object_get_ex(obj, "name", &json);

5. json_object_object_length()

  • 描述: 获取 json 对象中 键值对个数

  • 原型: int json_object_object_length(const struct json_object* obj);

  • 参数: json 对象

  • 返回: 返回个数

  • 示例:

#include 
#include 

int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
    /* 给 json 对象 添加内容 */
	json_object_object_add(obj, "name", json_object_new_string("jack"));
	json_object_object_add(obj, "age", json_object_new_int(10));
    
    int length = json_object_object_length(obj);
	return 0;
}

输出结果:

length = 2

6. json_object_to_json_string()

  • 描述: 把 json 对象内容转为字符串

  • 原型: const char* json_object_to_json_string(struct json_object *obj);

  • 参数: obj: json对象

  • 返回值: 字符串

  • 示例:

/* 将 json 对象内容 转为字符串 */
const char* buf = json_object_to_json_string(obj);
printf("%s\n", buf);

7. json_object_to_json_string_ext()

  • 描述: 把 json 对象内容转为字符串

  • 原型: const char* json_object_to_json_string_ext(struct json_object *obj, int flags);

  • 参数: 要转的 json 对象, flags: `一般写 2

  • 返回值: 字符串

  • 示例:

#include 
#include 

int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
    /* 给 json 对象 添加内容 */
    json_object_object_add(obj, "name", json_object_new_string("jack"));

    /* 将 json 对象内容 转为字符串 并且 按2个空格*/
    const char *buf = json_object_to_json_string_ext(obj, 2);
    printf("%s\n", buf);
	
	return 0;
}

输出结果:

  • 当 flags 参数 为 2
{
  "name":"jack"
}
  • 当 flags 参数 为 0
{"name":"jack"}

8. json_object_to_json_string_length()

  • 描述: 获取 json 对象 字符串长度

  • 原型: const char* json_object_to_json_string_length(struct json_object *obj, int flags, size_t *length);

  • 参数: obj: json对象, flags, length: 存放返回字符串的长度

  • 返回: 返回json对象字符串

  • 示例:

#include 
#include 

int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
    /* 给 json 对象 添加内容 */
    json_object_object_add(obj, "name", json_object_new_string("jack"));

    /* 将 json 对象 转成 字符串 并且 保存 获取 字符串长度 */
    size_t length;
    const char* buf = json_object_to_json_string_length(obj, 0, &length);
    printf("%s\n", buf);
    printf("length = %ld\n", length);
	
	return 0;
}

输出结果:

  • 当 flags = 0 时
{"name":"jack"}
length = 15
  • 当 flags = 2 时
{
  "name":"jack"
}
length = 19

9. json_object_is_type()

  • 描述: 判断json对象类型

  • 原型: int json_object_is_type(const struct json_object *obj, enum json_type type);

  • 参数: obj: 对象名, type: 类型

  • 返回: 是:返回 1, 否:返回 0

  • 示例

#include 
#include 

int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
    /* 判断 json 对象 是否是  json_type_object 类型 */
    int ret = json_object_is_type(obj, json_type_object);
    if(ret)
    {
        printf("obj 是 json_type_object 类型\n");
    }
    else
    {
        printf("obj 不是 json_type_object 类型\n");
    }
	
	return 0;
}

10. json_object_get_type()

  • 描述: 获取json对象类型

  • 原型: enum json_type json_object_get_type(const struct json_object *obj);

  • 参数: 要获取类型的 json 对象

  • 返回值: 返回对应的类型

  • 示例

#include 
#include 

/*
typedef enum json_type {
  json_type_null,               0
  json_type_boolean,            1
  json_type_double,             2
  json_type_int,                3
  json_type_object,             4
  json_type_array,              5
  json_type_string              6
} json_type;    
*/
int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
	/* 获取 json 对象 的类型 */
	json_type type = json_object_get_type(obj);
	printf("type = %d\n", type);
	
	return 0;
}

输出结果:

type = 4

11. json_object_get()

  • 描述: 获取 json 对象

  • 原型: struct json_object* json_object_get(struct json_object *obj);

  • 参数: 要获取的 json 对象

  • 返回值: json对象

  • 示例:

#include 
#include 

int main(int argc, char **argv)
{
	/* 创建一个 json 对象 */
	struct json_object *obj = json_object_new_object();
	
	/* 给 json 对象 添加 内容 */
	json_object_object_add(obj, "name", json_object_new_string("jack"));
	
	/*  使用json_object_get() 函数 复制 */
	struct json_object *new = json_object_get(obj);
	
	printf("obj: %s\n", json_object_get_string(obj));
	printf("new: %s\n", json_object_get_string(new));
	
	/* 其实目前 obj 和 new 是一样的了, 你对 obj 修改 其实 new 也会修改 对 new 修改 obj 也会 修改 */
	/* 下面演示: 对 obj 进行添加内容 其实 new 也是一样的 */
	json_object_object_add(obj, "age", json_object_new_int(18));
	
	printf("obj = %s\n", json_object_get_string(obj));
	printf("new = %s\n", json_object_get_string(new));
	
	return 0;
}
  • 输出结果:
obj: { "name": "jack" }
new: { "name": "jack" }
obj = { "name": "jack", "age": 18 }
new = { "name": "jack", "age": 18 }

12. json_object_new_array()

  • 描述: 创建一个 数组 json 对象

  • 原型: struct json_object *json_object_new_array(void);

  • 参数: 无

  • 返回: json对象

  • 示例:

/* 创建一个 json 对象 */
struct json_object *array = json_object_new_array();

13. json_object_array_add()

  • 描述:给数组 json 对象 添加键值

  • 原型: struct json_object *json_object_new_array(void);

  • 参数:

  • 返回: json 对象

  • 示例: [ 1, 2, 3 ]

/* 创建一个 数组 json 对象 */
struct json_object *array = json_object_new_array();
json_object_array_add(array, json_object_new_int(1));
json_object_array_add(array, json_object_new_int(2));
json_object_array_add(array, json_object_new_int(3));
  • 示例: [ { "name": "jack" } ]
/* 创建 json 对象 并 添加键值 */
struct json_object *obj = json_object_new_object();
json_object_object_add(obj, "name", json_object_new_string("jack"));

/* 创建数组 json 对象 并添加内容 */
struct json_object *array = json_object_new_array();
json_object_array_add(array, obj);

14. json_object_array_length()

  • 描述:获取数组 json 对象 内容长度

  • 原型: size_t json_object_array_length(const struct json_object *obj);

  • 参数: json对象

  • 返回: 长度

三. 编写 json-c 示例

下面的是先是构建数据格式, 然后再进行解析出来。

    1. { "name": "jack", "age": 18, "socre": 100 }
#include 
#include 

int main(int argc, char **argv)
{
	char *str = "{\"name\":\"jack\",\"age\":18,\"socre\":100}";
	//char *str = "{ \"name\": \"jack\", \"age\": \"18\", \"socre\": \"100\" }";
	
	/* 将符合 json 格式的字符串 转为 json 对象 */
	struct json_object *obj = json_tokener_parse(str);

	/* 把 json 对象 转为 字符串 */
	printf("%s\n", json_object_to_json_string(obj));
	
	return 0;
}

输出结果:

{ "name": "jack", "age": 18, "socre": 100 }
    1. { "name": "jack", "age": 18, "score": 100 }
#include 
#include 


int main(int argc, char **argv)
{
	struct json_object *obj = json_object_new_object();
		
	/*  { "name": "jack", "age": 18, "score": 100 } */
	/* 构建数据包 */
	json_object_object_add(obj, "name", json_object_new_string("jack"));
	json_object_object_add(obj, "age", json_object_new_int(18));
	json_object_object_add(obj, "score", json_object_new_int(100));

	/* json数据包转字符串输出 */
	printf("%s\n", json_object_to_json_string(obj));	


	/* 根据键名 解析出 json 对象 */
	struct json_object *json;
	json_object_object_get_ex(obj, "name", &json);
	
	/* 判断解析出的 json 对象 是什么类型 */
	json_type type = json_object_get_type(json);
	if(type == json_type_string)
	{
		printf("name: %s\n", json_object_get_string(json));
	}


	json_object_object_get_ex(obj, "age", &json);
	
	/* 判断解析出的 json 对象 是什么类型 */
	type = json_object_get_type(json);
	if(type == json_type_int)
	{
		printf("age: %d\n", json_object_get_int(json));
	}
	


	json_object_object_get_ex(obj, "score", &json);
	
	/* 判断解析出的 json 对象 是什么类型 */
	type = json_object_get_type(json);
	if(type == json_type_int)
	{
		printf("score: %d\n", json_object_get_int(json));
	}

	return 0;
}

输出结果:

{ "name": "jack", "age": 18, "score": 100 }
name: jack
age: 18
score: 100
    1. { "name": "jack", "score": [ 80, 90, 100 ] }
#include 
#include 

int main(int argc, char **argv)
{
	struct json_object *obj = json_object_new_object();

	json_object_object_add(obj, "name", json_object_new_string("jack"));

	/* 创建一个 json 数组对象 */
	struct json_object *array = json_object_new_array();

	json_object_array_add(array, json_object_new_int(80));
	json_object_array_add(array, json_object_new_int(90));
	json_object_array_add(array, json_object_new_int(100));

	/* 添加到 obj 对象中 */
	json_object_object_add(obj, "score", array);

	printf("%s\n", json_object_to_json_string(obj));



	/* 解析Json 数据 */
	struct json_object *json;
	json_object_object_get_ex(obj, "name", &json);
	printf("%s\n", json_object_get_string(json));


	json_object_object_get_ex(obj, "score", &json);
	
	if(json_object_get_type(json) == json_type_array)
	{
		int size = json_object_array_length(array);
		for(int i = 0; i < size; i++)
		{
			/* 根据下标提取json对象 */
			struct json_object *j = json_object_array_get_idx(json, i);
			if(json_object_get_type(j) == json_type_int)
			{
				printf("%d ", json_object_get_int(j));
			}
		}	
			printf("\n");
	}

	return 0;
}

输出结果:

{ "name": "jack", "score": [ 80, 90, 100 ] }
jack
80 90 100 
    1. { "score": [ { "math": 100 }, { "english": 88 } ] }
#include 
#include 

int main(int argc, char *argv)
{
	/**
	 * { "score": [{ "math": 100 } { "english": 88 }] }
	 */

	struct json_object *obj = json_object_new_object();
	struct json_object *array = json_object_new_array();


	struct json_object *o = json_object_new_object();
	json_object_object_add(o, "math", json_object_new_int(100));
	json_object_array_add(array, o);

	struct json_object *b = json_object_new_object();
	json_object_object_add(b, "english", json_object_new_int(88));
	json_object_array_add(array, b);

	json_object_object_add(obj, "score", array);

	printf("%s\n", json_object_get_string(obj));


	/* 解析 json 数据包 */

	struct json_object *json;
	
	json_object_object_get_ex(obj, "score", &json);

	int size = json_object_array_length(json);
	
	printf("size = %d\n", size);
	printf("%s\n", json_object_get_string(json));
	
	for(int i = 0; i < size; i++)
	{
		struct json_object *j = json_object_array_get_idx(json, i);
		if(json_object_get_type(j) == json_type_object)
		{
			struct json_object *t;
			if(i % 2 == 0)
			{
				json_object_object_get_ex(j, "math", &t);		
				printf("math: %d\n", json_object_get_int(t));
			}
			else
			{
				json_object_object_get_ex(j, "english", &t);
				printf("english: %d\n", json_object_get_int(t));
			}

		}
	}




	return 0;
}

输出结果:

{ "score": [ { "math": 100 }, { "english": 88 } ] }
size = 2
[ { "math": 100 }, { "english": 88 } ]
math: 100
english: 88

继续加油! 都看到这里了,这不得点赞评论一下? hh

你可能感兴趣的:(Linux,日常和工具,json,linux,c语言,ubuntu,c)