日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSON文件学习(jsonc、json-c)(不要学这个,去学cJSON)

發(fā)布時(shí)間:2025/3/20 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSON文件学习(jsonc、json-c)(不要学这个,去学cJSON) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

json-c從會(huì)使用到自實(shí)現(xiàn)

json-c和jsoncpp的api使用教程(附視頻鏈接)

文章目錄

  • json-c和jsoncpp的api使用教程(附視頻鏈接)
    • 1.json的介紹
    • 2.json格式的實(shí)例
    • 3.json-C的安裝
    • 4.json-C的api介紹
      • 創(chuàng)建一個(gè)引用計(jì)數(shù)為1的新空對(duì)象
      • 向類型為object的對(duì)象添加對(duì)象字段
      • 將c字符串轉(zhuǎn)換為json字符串格式的對(duì)象
      • 將整數(shù)轉(zhuǎn)換為json格式的對(duì)象
      • 將object對(duì)象轉(zhuǎn)化為json格式的字符串
      • 將字符串解析成json對(duì)象
      • 根據(jù)鍵名獲取對(duì)應(yīng)的json對(duì)象
      • 獲取json對(duì)象的整型值
      • 獲取json對(duì)象的字符串值
      • 獲取json對(duì)象類型
      • 創(chuàng)建一個(gè)json數(shù)組類型JSON對(duì)象
      • 往json_type_array類型的json對(duì)象中添加一個(gè)元素
      • 獲取json_type_array類型的對(duì)象中指定下標(biāo)的元素
    • 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 對(duì)象表示法(JavaScript Object Notation 【JavaScript對(duì)象標(biāo)記】)
  • JSON 是輕量級(jí)的文本數(shù)據(jù)交換格式
  • JSON 獨(dú)立于語言:JSON 使用 Javascript語法來描述數(shù)據(jù)對(duì)象,但是 JSON 仍然獨(dú)立于語言和平臺(tái)。JSON 解析器和 JSON 庫支持許多不同的編程語言。 目前非常多的動(dòng)態(tài)(PHP,JSP,.NET)編程語言都支持JSON。
  • JSON 具有自我描述性,更易理解

2.json格式的實(shí)例

中括號(hào){}代表一個(gè)JSON對(duì)象,JSON對(duì)象里能包含多個(gè)鍵值對(duì),用,分隔,鍵值對(duì)中的值中能為各種類型,包括數(shù)組(里面能存相同類型的多個(gè)元素,包括{}JSON對(duì)象),和{}JSON對(duì)象

myObj = {"name":"mr_zhou","num":3,"sites": [{ "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻譯" ] },{ "name":"Baidu", "info":[ "百度地圖", "百度AI", "百度翻譯" ] },{ "name":"Taobao", "info":[ "淘寶", "網(wǎng)購" ] }] }

json套json,鍵不管,值用[]括起來

3.json-C的安裝

Ubuntu操作系統(tǒng)

sudo apt-get install libjson0-dev libjson0

4.json-C的api介紹

創(chuàng)建一個(gè)引用計(jì)數(shù)為1的新空對(duì)象

struct json_object * object = json_object_new_object();

向類型為object的對(duì)象添加對(duì)象字段

int result = json_object_object_add(struct json_object *obj, const char *key, struct json_object *val);

將c字符串轉(zhuǎn)換為json字符串格式的對(duì)象

struct json_object * obj = json_object_new_string(const char *s);

將整數(shù)轉(zhuǎn)換為json格式的對(duì)象

struct json_object * obj = json_object_new_int(int32_t i);

將object對(duì)象轉(zhuǎn)化為json格式的字符串

const char * str = json_object_to_json_string(struct json_object *obj);

將字符串解析成json對(duì)象

struct json_object * json = json_tokener_parse(const char *str);

根據(jù)鍵名獲取對(duì)應(yīng)的json對(duì)象

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

獲取json對(duì)象的整型值

int32_t res = json_object_get_int(const struct json_object *obj);

獲取json對(duì)象的字符串值

const char * str = json_object_get_string(struct json_object *obj);

獲取json對(duì)象類型

enum json_type type = json_object_get_type(const struct json_object *obj);

創(chuàng)建一個(gè)json數(shù)組類型JSON對(duì)象

struct json_object * array = json_object_new_array();

往json_type_array類型的json對(duì)象中添加一個(gè)元素

int res = json_object_array_add(struct json_object *obj, struct json_object *val);

獲取json_type_array類型的對(duì)象中指定下標(biāo)的元素

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的官方代碼庫。 請(qǐng)參見wiki以獲取下載的發(fā)布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 實(shí)現(xiàn)了一個(gè)引用計(jì)數(shù)對(duì)象模型,使您可以輕松地在 C 中構(gòu)造 JSON 對(duì)象,將它們輸出為 JSON 格式的字符串,并將 JSON 格式的字符串解析回 JSON 對(duì)象的 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.

如果您已經(jīng)安裝了 json-c 并準(zhǔn)備好使用,請(qǐng)?zhí)潦褂?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.

如果您已經(jīng)安裝了 json-c,請(qǐng)參閱鏈接到 libjson-c,了解如何構(gòu)建和鏈接您的程序。

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 # optional

Build 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 arguments

Note: 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 install

Generating 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.html

CMake 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.

# build a static library only cmake -DBUILD_SHARED_LIBS=OFF ..

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 make

cmake-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=0

To 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 valgrind

If 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 test

or

JSONC_TEST_TRACE=1 make test

and 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-c

The 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-c

If your project uses cmake:

  • Add to your CMakeLists.txt file:
find_package(json-c CONFIG) target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)
  • Then you might run in your project:
cd build cmake -DCMAKE_PREFIX_PATH=/path/to/json_c/install/lib64/cmake ..

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 對(duì)象樹的方法。
  • json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving objects from a json-c object tree. 用于從 json-c 對(duì)象樹中檢索對(duì)象的 JSON 指針 (RFC 6901) 實(shí)現(xiàn)。
  • json_object_iterator.h - Methods for iterating over single json_object instances. (See also json_object_object_foreach() in json_object.h) 迭代單個(gè) json_object 實(shí)例的方法。 (另見 json_object.h 中的 json_object_object_foreach())
  • json_visit.h - Methods for walking a tree of json-c objects. 遍歷 json-c 對(duì)象樹的方法。
  • json_util.h - Miscellaneous utility functions. 雜項(xiàng)效用函數(shù)。
    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。 它描述了一個(gè) json 對(duì)象的引用計(jì)數(shù)樹,這些對(duì)象是通過使用 json_tokener(即 json_tokener_parse_ex())解析文本或通過創(chuàng)建(使用 json_object_new_object()、json_object_new_int() 等)并添加(使用 json_object_object_add( ), json_object_array_add(), 等等…) 它們分別。 通常,樹中的每個(gè)對(duì)象都有一個(gè)來自其父對(duì)象的引用。 完成對(duì)象樹后,您只需在根對(duì)象上調(diào)用 json_object_put() 以釋放它,這會(huì)通過依次在每個(gè)子對(duì)象上調(diào)用 json_object_put() 的任何子對(duì)象向下遞歸。

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())

您可以獲得對(duì)單個(gè)子對(duì)象(json_object_object_get() 或 json_object_array_get_idx())的引用,并使用該對(duì)象,只要其父對(duì)象有效。
如果您需要一個(gè)子對(duì)象的壽命比其父對(duì)象長(zhǎng),您可以增加子對(duì)象的引用計(jì)數(shù) (json_object_get()) 以允許它在父對(duì)象被釋放或從其父對(duì)象中移除時(shí)存活下來(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.

解析文本時(shí),json_tokener 對(duì)象獨(dú)立于它返回的 json_object。 它可以被分配 (json_tokener_new()) 使用一次或多次 (json_tokener_parse_ex(),并在 json_object 對(duì)象存在時(shí)釋放 (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 樹序列化回字符串。 返回的字符串僅在對(duì)同一對(duì)象進(jìn)行下一次“to_json_string”調(diào)用之前有效。 此外,它會(huì)在 json_object 被釋放時(shí)被釋放。

總結(jié)

以上是生活随笔為你收集整理的JSON文件学习(jsonc、json-c)(不要学这个,去学cJSON)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。