javascript
JSON文件学习(jsonc、json-c)(不要学这个,去学cJSON)
json-c從會使用到自實現
json-c和jsoncpp的api使用教程(附視頻鏈接)
文章目錄
- json-c和jsoncpp的api使用教程(附視頻鏈接)
- 1.json的介紹
- 2.json格式的實例
- 3.json-C的安裝
- 4.json-C的api介紹
- 創建一個引用計數為1的新空對象
- 向類型為object的對象添加對象字段
- 將c字符串轉換為json字符串格式的對象
- 將整數轉換為json格式的對象
- 將object對象轉化為json格式的字符串
- 將字符串解析成json對象
- 根據鍵名獲取對應的json對象
- 獲取json對象的整型值
- 獲取json對象的字符串值
- 獲取json對象類型
- 創建一個json數組類型JSON對象
- 往json_type_array類型的json對象中添加一個元素
- 獲取json_type_array類型的對象中指定下標的元素
- 5.使用json-C的例子
- Github:json-c/json-c
- JSON-C - A JSON implementation in C
- Building on Unix with git, gcc and cmake
- Prerequisites:
- Install using apt (e.g. Ubuntu 16.04.2 LTS)
- Build instructions:
- Generating documentation with Doxygen:
- CMake Options
- Building with partial threading support
- cmake-configure wrapper script
- Testing:
- Building on Unix and Windows with vcpkg
- Linking to libjson-c
- Using json-c
json-c和jsoncpp的api使用教程(附視頻鏈接)
1.json的介紹
- JSON 指的是 JavaScript 對象表示法(JavaScript Object Notation 【JavaScript對象標記】)
- JSON 是輕量級的文本數據交換格式
- JSON 獨立于語言:JSON 使用 Javascript語法來描述數據對象,但是 JSON 仍然獨立于語言和平臺。JSON 解析器和 JSON 庫支持許多不同的編程語言。 目前非常多的動態(PHP,JSP,.NET)編程語言都支持JSON。
- JSON 具有自我描述性,更易理解
2.json格式的實例
中括號{}代表一個JSON對象,JSON對象里能包含多個鍵值對,用,分隔,鍵值對中的值中能為各種類型,包括數組(里面能存相同類型的多個元素,包括{}JSON對象),和{}JSON對象
myObj = {"name":"mr_zhou","num":3,"sites": [{ "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻譯" ] },{ "name":"Baidu", "info":[ "百度地圖", "百度AI", "百度翻譯" ] },{ "name":"Taobao", "info":[ "淘寶", "網購" ] }] }json套json,鍵不管,值用[]括起來
3.json-C的安裝
Ubuntu操作系統
sudo apt-get install libjson0-dev libjson04.json-C的api介紹
創建一個引用計數為1的新空對象
struct json_object * object = json_object_new_object();向類型為object的對象添加對象字段
int result = json_object_object_add(struct json_object *obj, const char *key, struct json_object *val);將c字符串轉換為json字符串格式的對象
struct json_object * obj = json_object_new_string(const char *s);將整數轉換為json格式的對象
struct json_object * obj = json_object_new_int(int32_t i);將object對象轉化為json格式的字符串
const char * str = json_object_to_json_string(struct json_object *obj);將字符串解析成json對象
struct json_object * json = json_tokener_parse(const char *str);根據鍵名獲取對應的json對象
json_bool res = json_object_object_get_ex(const struct json_object *obj, const char *key, struct json_object **value);獲取json對象的整型值
int32_t res = json_object_get_int(const struct json_object *obj);獲取json對象的字符串值
const char * str = json_object_get_string(struct json_object *obj);獲取json對象類型
enum json_type type = json_object_get_type(const struct json_object *obj);創建一個json數組類型JSON對象
struct json_object * array = json_object_new_array();往json_type_array類型的json對象中添加一個元素
int res = json_object_array_add(struct json_object *obj, struct json_object *val);獲取json_type_array類型的對象中指定下標的元素
struct json_object * obj3 = json_object_array_get_idx(const struct json_object *obj, size_t idx);5.使用json-C的例子
使用的json格式字符串是:{"name":"mr_zhou","age":20,"score":[{"chinese":100},{"chinese:"80},{"chinese":90}]}
Github:json-c/json-c
https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/ https://github.com/json-c/json-c是json-c的官方代碼庫。 請參見wiki以獲取下載的發布tarballs。API文檔位于http://json-c.github.io/json-c/JSON-C - A JSON implementation in C
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to RFC 7159.
JSON-C 實現了一個引用計數對象模型,使您可以輕松地在 C 中構造 JSON 對象,將它們輸出為 JSON 格式的字符串,并將 JSON 格式的字符串解析回 JSON 對象的 C 表示形式。 它旨在符合 RFC 7159。
Skip down to Using json-c or check out the API docs, if you already have json-c installed and ready to use.
如果您已經安裝了 json-c 并準備好使用,請跳至使用 json-c 或查看 API 文檔。
Home page for json-c: https://github.com/json-c/json-c/wiki
Build Status:
- AppVeyor Build
- Travis Build
Test Status
- Coveralls Coverage Status
Building on Unix with git, gcc and cmake
If you already have json-c installed, see Linking to libjson-c for how to build and link your program against it.
如果您已經安裝了 json-c,請參閱鏈接到 libjson-c,了解如何構建和鏈接您的程序。
Prerequisites:
-
gcc, clang, or another C compiler
-
cmake>=2.8, >=3.16 recommended, cmake=>3.1 for tests
To generate docs you’ll also need:
- doxygen>=1.8.13
If you are on a relatively modern system, you’ll likely be able to install the prerequisites using your OS’s packaging system.
Install using apt (e.g. Ubuntu 16.04.2 LTS)
sudo apt install git sudo apt install cmake sudo apt install doxygen # optional sudo apt install valgrind # optionalBuild instructions:
json-c GitHub repo: https://github.com/json-c/json-c
$ git clone https://github.com/json-c/json-c.git $ mkdir json-c-build $ cd json-c-build $ cmake ../json-c # See CMake section below for custom argumentsNote: it’s also possible to put your build directory inside the json-c source directory, or even not use a separate build directory at all, but certain things might not work quite right (notably, make distcheck)
Then:
$ make $ make test $ make USE_VALGRIND=0 test # optionally skip using valgrind $ make installGenerating documentation with Doxygen:
The library documentation can be generated directly from the source code using Doxygen tool:
# in build directory make doc google-chrome doc/html/index.htmlCMake Options
The json-c library is built with CMake, which can take a few options.
Pass these options as -D on CMake’s command-line.
Building with partial threading support
Although json-c does not support fully multi-threaded access to object trees, it has some code to help make its use in threaded programs a bit safer. Currently, this is limited to using atomic operations for json_object_get() and json_object_put().
Since this may have a performance impact, of at least 3x slower according to https://stackoverflow.com/a/11609063, it is disabled by default. You may turn it on by adjusting your cmake command with: -DENABLE_THREADING=ON
Separately, the default hash function used for object field keys, lh_char_hash, uses a compare-and-swap operation to ensure the random seed is only generated once. Because this is a one-time operation, it is always compiled in when the compare-and-swap operation is available.
cmake-configure wrapper script
For those familiar with the old autoconf/autogen.sh/configure method, there is a cmake-configure wrapper script to ease the transition to cmake.
mkdir build cd build ../cmake-configure --prefix=/some/install/path makecmake-configure can take a few options.
Testing:
By default, if valgrind is available running tests uses it. That can slow the tests down considerably, so to disable it use:
export USE_VALGRIND=0To run tests a separate build directory is recommended:
mkdir build-test cd build-test # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code # which uses slightly slower, but valgrind-safe code. VALGRIND=1 cmake .. makemake test # By default, if valgrind is available running tests uses it. make USE_VALGRIND=0 test # optionally skip using valgrindIf a test fails, check Testing/Temporary/LastTest.log, tests/testSubDir/${testname}/${testname}.vg.out, and other similar files. If there is insufficient output try:
VERBOSE=1 CTEST_OUTPUT_ON_FAILURE=1 make testor
JSONC_TEST_TRACE=1 make testand check the log files again.
Building on Unix and Windows with vcpkg
You can download and install JSON-C using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install vcpkg install json-cThe JSON-C port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Linking to libjson-c
If your system has pkgconfig, then you can just add this to your makefile:
CFLAGS += $(shell pkg-config --cflags json-c) LDFLAGS += $(shell pkg-config --libs json-c)Without pkgconfig, you might do something like this:
JSON_C_DIR=/path/to/json_c/install CFLAGS += -I$(JSON_C_DIR)/include/json-c # Or to use lines like: #include <json-c/json_object.h> #CFLAGS += -I$(JSON_C_DIR)/include LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-cIf your project uses cmake:
- Add to your CMakeLists.txt file:
- Then you might run in your project:
Using json-c
To use json-c you can either include json.h, or preferably, one of the following more specific header files:
要使用 json-c,您可以包含 json.h,或者最好包含以下更具體的頭文件之一:
- json_object.h - Core types and methods. 核心類型和方法。
- json_tokener.h - Methods for parsing and serializing json-c object trees. 解析和序列化 json-c 對象樹的方法。
- json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving objects from a json-c object tree. 用于從 json-c 對象樹中檢索對象的 JSON 指針 (RFC 6901) 實現。
- json_object_iterator.h - Methods for iterating over single json_object instances. (See also json_object_object_foreach() in json_object.h) 迭代單個 json_object 實例的方法。 (另見 json_object.h 中的 json_object_object_foreach())
- json_visit.h - Methods for walking a tree of json-c objects. 遍歷 json-c 對象樹的方法。
- json_util.h - Miscellaneous utility functions. 雜項效用函數。
For a full list of headers see files.html
The primary type in json-c is json_object. It describes a reference counted tree of json objects which are created by either parsing text with a json_tokener (i.e. json_tokener_parse_ex()), or by creating (with json_object_new_object(), json_object_new_int(), etc…) and adding (with json_object_object_add(), json_object_array_add(), etc…) them individually. Typically, every object in the tree will have one reference, from its parent. When you are done with the tree of objects, you call json_object_put() on just the root object to free it, which recurses down through any child objects calling json_object_put() on each one of those in turn.
json-c 中的主要類型是 json_object。 它描述了一個 json 對象的引用計數樹,這些對象是通過使用 json_tokener(即 json_tokener_parse_ex())解析文本或通過創建(使用 json_object_new_object()、json_object_new_int() 等)并添加(使用 json_object_object_add( ), json_object_array_add(), 等等…) 它們分別。 通常,樹中的每個對象都有一個來自其父對象的引用。 完成對象樹后,您只需在根對象上調用 json_object_put() 以釋放它,這會通過依次在每個子對象上調用 json_object_put() 的任何子對象向下遞歸。
You can get a reference to a single child (json_object_object_get() or json_object_array_get_idx()) and use that object as long as its parent is valid.
If you need a child object to live longer than its parent, you can increment the child’s refcount (json_object_get()) to allow it to survive the parent being freed or it being removed from its parent (json_object_object_del() or json_object_array_del_idx())
您可以獲得對單個子對象(json_object_object_get() 或 json_object_array_get_idx())的引用,并使用該對象,只要其父對象有效。
如果您需要一個子對象的壽命比其父對象長,您可以增加子對象的引用計數 (json_object_get()) 以允許它在父對象被釋放或從其父對象中移除時存活下來(json_object_object_del() 或 json_object_array_del_idx())
When parsing text, the json_tokener object is independent from the json_object that it returns. It can be allocated (json_tokener_new()) used one or multiple times (json_tokener_parse_ex(), and freed (json_tokener_free()) while the json_object objects live on.
解析文本時,json_tokener 對象獨立于它返回的 json_object。 它可以被分配 (json_tokener_new()) 使用一次或多次 (json_tokener_parse_ex(),并在 json_object 對象存在時釋放 (json_tokener_free())。
A json_object tree can be serialized back into a string with json_object_to_json_string_ext(). The string that is returned is only valid until the next “to_json_string” call on that same object. Also, it is freed when the json_object is freed.
可以使用 json_object_to_json_string_ext() 將 json_object 樹序列化回字符串。 返回的字符串僅在對同一對象進行下一次“to_json_string”調用之前有效。 此外,它會在 json_object 被釋放時被釋放。
總結
以上是生活随笔為你收集整理的JSON文件学习(jsonc、json-c)(不要学这个,去学cJSON)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ubuntu pkg-con
- 下一篇: C语言中的隐式声明是什么,有什么危害?w