Mac安装 Json-c

来源:互联网 发布:ppt制作软件下载 编辑:程序博客网 时间:2024/06/07 02:50

Mac安装 Json-c

什么是 JSON ?

JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格式。JSO N 与 XML 具有相同的特性,例如易于人编写和阅读,易于机器生成和解析。但是JSON 比 XML 数据传输的有效性要高出很多。JSON 完全独立与编程语言,使用文本格式保存。

JSON 数据有两种结构:
• Name-Value 对构成的集合,类似于 Java 中的 Map。
• Value 的有序列表,类似于 Java 中的 Array。
一个 JSON 格式的数据示例:
[
{
“precision”: “zip”,
“Latitude”: 37.7668,
“Longitude”: -122.3959,
“Address”: “”,
“City”: “SAN FRANCISCO”,
“State”: “CA”,
“Zip”: “94107”,
“Country”: “US”
},
{
“precision”: “zip”,
“Latitude”: 37.371991,
“Longitude”: -122.026020,
“Address”: “”,
“City”: “SUNNYVALE”,
“State”: “CA”,
“Zip”: “94085”,
“Country”: “US”
}
]

安装 Json-c

有两种安装方式:

HomeBrew 安装

直接使用命令:brew install json-c

Git 安装

这种安装方式因为要编译源文件,所以要费点劲。
首先你需要安装 autoTools 工具,执行以下命令:

brew install automakebrew install libtool

从 Git 下载代码包 json-c。解压后查看里面的README 文件:

Building on Unix with gcc and autotoolsHome page for json-c:  http://oss.metaparadigm.com/json-c/If checking out from SVN (http://svn.metaparadigm.com/svn/json-c/trunk) or Git (https://github.com/jehiah/json-c):    $ sh autogen.shThen     $ ./configure    $ make    $ make installTo build and run the test programs run     $ make checkLinking to libjsonIf your system has pkgconfig then you can just add this to your makefileCFLAGS += $(shell pkg-config --cflags json)LDFLAGS += $(shell pkg-config --libs json)

大概意思就是说在安装好autotools工具后,依次执行 以下命令

 sh autogen.sh./configuremakemake installmake check

在编译 json 程序时,在 makefile 中添加 Json 依赖项:

JSON_C_DIR=/path/to/json_c/install #安装目录一般为/usr/local/CFLAGS += -I$(JSON_C_DIR)/include/json-cLDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c

测试程序

#include <json-c/json.h>#include <stdio.h>int main(int argc, char* argv[]){        char *string = "{\"name\": \"joys of programming\"}";        json_object *jobj = json_tokener_parse(string);        enum json_type type = json_object_get_type(jobj);        printf("Type: ");        switch(type) {        case json_type_null:                printf("json_type_null\n");                break;        case json_type_boolean:                printf("json_type_boolean\n");                break;        case json_type_double:                printf("json_type_double\n");                break;        case json_type_int:                printf("json_type_int\n");                break;        case json_type_object:                printf("json_type_object\n");                break;        case json_type_array:                printf("json_type_array\n");                break;        case json_type_string:                printf("json_type_string\n");                break;        }           return 0;}

上面的测试程序创建一个 json_object对象,并将结果的数据类型输出至 stdout。编译运行:

gcc -o proc demo1.c  -ljson

这里写图片描述

参考链接:

Introducing JSON
Linux安装JSON-C
JSON 教程 偏向 Html
JSON 教程 偏向C编程

原创粉丝点击