Linux json-c 安装和使用
Posted 皮卡丘吉尔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux json-c 安装和使用相关的知识,希望对你有一定的参考价值。
json-c安装和使用
- json-c
- 一. 安装 json-c
- 二. 使用 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. 命令安装
-
sudo apt-get update
(非必须)
-
sudo apt-get install libjson-c-dev
2. 下载源码安装
-
- 打开网址链接
-
- 下载源码
-
方式1: 直接下载zip
-
方式2: 用
git
命令下载-
git clone https://github.com/json-c/json-c.git
-
- 创建
build
目录mkdir build
- 创建
-
- 进入
build
目录cd build/
- 进入
-
- 执行
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$
-
- 可以
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$
-
- 执行 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$
-
- 执行
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 <json-c/json.h> 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$
-
- 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$
-
- 查看文件信息
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$
-
- 添加到
/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以上是关于Linux json-c 安装和使用的主要内容,如果未能解决你的问题,请参考以下文章
JSON文件学习(jsoncjson-c)(不要学这个,去学cJSON)
c_cpp 使用libcurl和json-c发布和解析来自http://jsonplaceholder.typicode.com的返回的示例代码