cJSON的安装和使用
文章目录
- 前言
- 1.下载cJSON-master
- 2.代码示例
- 3.编译运行
- 总结
前言
cJSON 是一个使用标准 C 编写的轻量级 JSON 解析器/生成器,其全部功能仅由 cJSON.c 和 cJSON.h 两个文件实现,因此极易集成到嵌入式、物联网或任何资源受限的 C 项目中。
1.下载cJSON-master
下载地址列在这里
直接下载 zip 后解压,即得到 cJSON-master 目录。
编译安装
unzip cJSON-master.zip
cd cJSON-master
mkdir build && cd build
cmake .. # 默认生成静态/动态库
xxx@ubuntu:/xxx/cjson_test/cJSON-master/build$ cmake ..
-- The C compiler identification is GNU 5.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
-- Performing Test FLAG_SUPPORTED_stdc89
-- Performing Test FLAG_SUPPORTED_stdc89 - Success
-- Performing Test FLAG_SUPPORTED_pedantic
-- Performing Test FLAG_SUPPORTED_pedantic - Success
-- Performing Test FLAG_SUPPORTED_Wall
-- Performing Test FLAG_SUPPORTED_Wall - Success
-- Performing Test FLAG_SUPPORTED_Wextra
-- Performing Test FLAG_SUPPORTED_Wextra - Success
-- Performing Test FLAG_SUPPORTED_Werror
-- Performing Test FLAG_SUPPORTED_Werror - Success
-- Performing Test FLAG_SUPPORTED_Wstrictprototypes
-- Performing Test FLAG_SUPPORTED_Wstrictprototypes - Success
-- Performing Test FLAG_SUPPORTED_Wwritestrings
-- Performing Test FLAG_SUPPORTED_Wwritestrings - Success
-- Performing Test FLAG_SUPPORTED_Wshadow
-- Performing Test FLAG_SUPPORTED_Wshadow - Success
-- Performing Test FLAG_SUPPORTED_Winitself
-- Performing Test FLAG_SUPPORTED_Winitself - Success
-- Performing Test FLAG_SUPPORTED_Wcastalign
-- Performing Test FLAG_SUPPORTED_Wcastalign - Success
-- Performing Test FLAG_SUPPORTED_Wformat2
-- Performing Test FLAG_SUPPORTED_Wformat2 - Success
-- Performing Test FLAG_SUPPORTED_Wmissingprototypes
-- Performing Test FLAG_SUPPORTED_Wmissingprototypes - Success
-- Performing Test FLAG_SUPPORTED_Wstrictoverflow2
-- Performing Test FLAG_SUPPORTED_Wstrictoverflow2 - Success
-- Performing Test FLAG_SUPPORTED_Wcastqual
-- Performing Test FLAG_SUPPORTED_Wcastqual - Success
-- Performing Test FLAG_SUPPORTED_Wundef
-- Performing Test FLAG_SUPPORTED_Wundef - Success
-- Performing Test FLAG_SUPPORTED_Wswitchdefault
-- Performing Test FLAG_SUPPORTED_Wswitchdefault - Success
-- Performing Test FLAG_SUPPORTED_Wconversion
-- Performing Test FLAG_SUPPORTED_Wconversion - Success
-- Performing Test FLAG_SUPPORTED_Wccompat
-- Performing Test FLAG_SUPPORTED_Wccompat - Success
-- Performing Test FLAG_SUPPORTED_fstackprotectorstrong
-- Performing Test FLAG_SUPPORTED_fstackprotectorstrong - Success
-- Performing Test FLAG_SUPPORTED_Wcomma
-- Performing Test FLAG_SUPPORTED_Wcomma - Failed
-- Performing Test FLAG_SUPPORTED_Wdoublepromotion
-- Performing Test FLAG_SUPPORTED_Wdoublepromotion - Success
-- Performing Test FLAG_SUPPORTED_Wparentheses
-- Performing Test FLAG_SUPPORTED_Wparentheses - Success
-- Performing Test FLAG_SUPPORTED_Wformatoverflow
-- Performing Test FLAG_SUPPORTED_Wformatoverflow - Failed
-- Performing Test FLAG_SUPPORTED_Wunusedmacros
-- Performing Test FLAG_SUPPORTED_Wunusedmacros - Success
-- Performing Test FLAG_SUPPORTED_Wmissingvariabledeclarations
-- Performing Test FLAG_SUPPORTED_Wmissingvariabledeclarations - Failed
-- Performing Test FLAG_SUPPORTED_Wusedbutmarkedunused
-- Performing Test FLAG_SUPPORTED_Wusedbutmarkedunused - Failed
-- Performing Test FLAG_SUPPORTED_Wswitchenum
-- Performing Test FLAG_SUPPORTED_Wswitchenum - Success
-- Performing Test FLAG_SUPPORTED_fvisibilityhidden
-- Performing Test FLAG_SUPPORTED_fvisibilityhidden - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /xxx/cjson_test/cJSON-master/buildmake
xxx@ubuntu:~/xxx/cJSON-master/build$ make
Scanning dependencies of target cjson
[ 2%] Building C object CMakeFiles/cjson.dir/cJSON.c.o
[ 4%] Linking C shared library libcjson.so
[ 4%] Built target cjson
Scanning dependencies of target cJSON_test
[ 6%] Building C object CMakeFiles/cJSON_test.dir/test.c.o
[ 8%] Linking C executable cJSON_test
[ 8%] Built target cJSON_test
Scanning dependencies of target unity
[ 11%] Building C object tests/CMakeFiles/unity.dir/unity/src/unity.c.o
[ 13%] Linking C static library libunity.a
[ 13%] Built target unity
Scanning dependencies of target print_number
[ 15%] Building C object tests/CMakeFiles/print_number.dir/print_number.c.o
[ 17%] Linking C executable print_number
[ 17%] Built target print_number
Scanning dependencies of target minify_tests
[ 20%] Building C object tests/CMakeFiles/minify_tests.dir/minify_tests.c.o
[ 22%] Linking C executable minify_tests
[ 22%] Built target minify_tests
Scanning dependencies of target parse_value
[ 24%] Building C object tests/CMakeFiles/parse_value.dir/parse_value.c.o
[ 26%] Linking C executable parse_value
[ 26%] Built target parse_value
Scanning dependencies of target parse_string
[ 28%] Building C object tests/CMakeFiles/parse_string.dir/parse_string.c.o
[ 31%] Linking C executable parse_string
[ 31%] Built target parse_string
Scanning dependencies of target parse_hex4
[ 33%] Building C object tests/CMakeFiles/parse_hex4.dir/parse_hex4.c.o
[ 35%] Linking C executable parse_hex4
[ 35%] Built target parse_hex4
Scanning dependencies of target print_string
[ 37%] Building C object tests/CMakeFiles/print_string.dir/print_string.c.o
[ 40%] Linking C executable print_string
[ 40%] Built target print_string
Scanning dependencies of target print_value
[ 42%] Building C object tests/CMakeFiles/print_value.dir/print_value.c.o
[ 44%] Linking C executable print_value
[ 44%] Built target print_value
Scanning dependencies of target parse_object
[ 46%] Building C object tests/CMakeFiles/parse_object.dir/parse_object.c.o
[ 48%] Linking C executable parse_object
[ 48%] Built target parse_object
Scanning dependencies of target parse_with_opts
[ 51%] Building C object tests/CMakeFiles/parse_with_opts.dir/parse_with_opts.c.o
[ 53%] Linking C executable parse_with_opts
[ 53%] Built target parse_with_opts
Scanning dependencies of target cjson_add
[ 55%] Building C object tests/CMakeFiles/cjson_add.dir/cjson_add.c.o
[ 57%] Linking C executable cjson_add
[ 57%] Built target cjson_add
Scanning dependencies of target parse_examples
[ 60%] Building C object tests/CMakeFiles/parse_examples.dir/parse_examples.c.o
[ 62%] Linking C executable parse_examples
[ 62%] Built target parse_examples
Scanning dependencies of target parse_number
[ 64%] Building C object tests/CMakeFiles/parse_number.dir/parse_number.c.o
[ 66%] Linking C executable parse_number
[ 66%] Built target parse_number
Scanning dependencies of target print_array
[ 68%] Building C object tests/CMakeFiles/print_array.dir/print_array.c.o
[ 71%] Linking C executable print_array
[ 71%] Built target print_array
Scanning dependencies of target print_object
[ 73%] Building C object tests/CMakeFiles/print_object.dir/print_object.c.o
[ 75%] Linking C executable print_object
[ 75%] Built target print_object
Scanning dependencies of target compare_tests
[ 77%] Building C object tests/CMakeFiles/compare_tests.dir/compare_tests.c.o
[ 80%] Linking C executable compare_tests
[ 80%] Built target compare_tests
Scanning dependencies of target misc_tests
[ 82%] Building C object tests/CMakeFiles/misc_tests.dir/misc_tests.c.o
[ 84%] Linking C executable misc_tests
[ 84%] Built target misc_tests
Scanning dependencies of target readme_examples
[ 86%] Building C object tests/CMakeFiles/readme_examples.dir/readme_examples.c.o
[ 88%] Linking C executable readme_examples
[ 88%] Built target readme_examples
Scanning dependencies of target parse_array
[ 91%] Building C object tests/CMakeFiles/parse_array.dir/parse_array.c.o
[ 93%] Linking C executable parse_array
[ 93%] Built target parse_array
Scanning dependencies of target fuzz_main
[ 95%] Building C object fuzzing/CMakeFiles/fuzz_main.dir/fuzz_main.c.o
[ 97%] Building C object fuzzing/CMakeFiles/fuzz_main.dir/cjson_read_fuzzer.c.o
[100%] Linking C executable fuzz_main
[100%] Built target fuzz_mainsudo make install # 把头文件和库装到系统目录
xxx@ubuntu:~/xxx/cJSON-master/build$ sudo make install
[ 4%] Built target cjson
[ 8%] Built target cJSON_test
[ 13%] Built target unity
[ 17%] Built target print_number
[ 22%] Built target minify_tests
[ 26%] Built target parse_value
[ 31%] Built target parse_string
[ 35%] Built target parse_hex4
[ 40%] Built target print_string
[ 44%] Built target print_value
[ 48%] Built target parse_object
[ 53%] Built target parse_with_opts
[ 57%] Built target cjson_add
[ 62%] Built target parse_examples
[ 66%] Built target parse_number
[ 71%] Built target print_array
[ 75%] Built target print_object
[ 80%] Built target compare_tests
[ 84%] Built target misc_tests
[ 88%] Built target readme_examples
[ 93%] Built target parse_array
[100%] Built target fuzz_main
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include/cjson/cJSON.h
-- Installing: /usr/local/lib/pkgconfig/libcjson.pc
-- Installing: /usr/local/lib/libcjson.so.1.7.19
-- Installing: /usr/local/lib/libcjson.so.1
-- Installing: /usr/local/lib/libcjson.so
-- Installing: /usr/local/lib/cmake/cJSON/cjson.cmake
-- Installing: /usr/local/lib/cmake/cJSON/cjson-noconfig.cmake
-- Installing: /usr/local/lib/cmake/cJSON/cJSONConfig.cmake
-- Installing: /usr/local/lib/cmake/cJSON/cJSONConfigVersion.cmake完成后会在 /usr/local/lib 得到 libcjson.so(或 .a),头文件位于 /usr/local/include/cJSON.h。
2.代码示例
#include <stdio.h>
#include "cJSON.h"int main(void)
{const char *json_str = "{\"name\":\"Alice\",\"age\":20}";cJSON *root = cJSON_Parse(json_str);if (!root) { puts("parse fail"); return 1; }cJSON *name = cJSON_GetObjectItem(root, "name");printf("name = %s\n", name->valuestring);cJSON *age = cJSON_GetObjectItem(root, "age");printf("age = %d\n", age->valueint);cJSON_Delete(root);return 0;
}
3.编译运行
#include <stdio.h>
#include "cJSON.h"int main(void)
{const char *json_str = "{\"name\":\"Alice\",\"age\":20}";cJSON *root = cJSON_Parse(json_str);if (!root) { puts("parse fail"); return 1; }cJSON *name = cJSON_GetObjectItem(root, "name");printf("name = %s\n", name->valuestring);cJSON *age = cJSON_GetObjectItem(root, "age");printf("age = %d\n", age->valueint);cJSON_Delete(root);return 0;
}gcc test_cjson.c -o test_cjson -I/usr/local/include/cjson -L/usr/local/lib -lcjson
xxx@ubuntu:~/xxx/cJSON-master/test_demo$ 运行的时候出现如下错误:原因:找不到库文件,解决方法使用sudo ldconfig解决问题。
xxx@ubuntu:~/xxx/cJSON-master/test_demo$ ./test_cjson
./test_cjson: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory
xxx@ubuntu:~/my_software/cJSON-master/test_demo$
xxx@ubuntu:~/my_software/cJSON-master/test_demo$
xxx@ubuntu:~/my_software/cJSON-master/test_demo$ ldconfig
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied
xxx@ubuntu:~/my_software/cJSON-master/test_demo$
xxx@ubuntu:~/my_software/cJSON-master/test_demo$
xxx@ubuntu:~/my_software/cJSON-master/test_demo$ sudo ldconfig
xxx@ubuntu:~/my_software/cJSON-master/test_demo$
xxx@ubuntu:~/my_software/cJSON-master/test_demo$ gcc test_cjson.c -o test_cjson -I/usr/local/include/cjson -L/usr/local/lib -lcjson
xxx@ubuntu:~/my_software/cJSON-master/test_demo$ ./test_cjson
name = Alice
age = 20
运行成功。
总结
对于资源有限的嵌入式应用,使用cjson解析数据非常方便。最近项目中用到这种方法,记录一下。以上就是在嵌入式处理器中使用cjson解析数据的方法。