您的位置:首页 > Web前端 > JavaScript

JSON-C结构简介、使用

2015-08-12 19:10 603 查看
官方站点介绍http://www.json.org

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It
is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript
Programming Language, Standard
ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family
of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON结构体例如以下:

typedefstruct cJSON {

structcJSON *next,*prev;

struct cJSON *child;

int type;

char * valuestring;

int valueint;

double valuedouble;

char *string;

}cJSON;

1、cJSON存储的时候是採用链表存储的,其訪问方式像一颗树。每个节点能够有兄妹节点和子节点。通过next/prev指针来查找上一节点或者下一节点。每个节点通过child指针来訪问,进入下一层。

2、type一共同拥有7种取值,各自是:

#define cJSON_False 0

#define cJSON_True 1

#define cJSON_NULL 2

#define cJSON_Number 3

#define cJSON_String 4

#define cJSON_Array 5

#define cJSON_Object 6

得到其职:

若是Number类型。则valueint或valuedouble中存储着值。若你期望的是int,则訪问valueint,若期望的是double。则訪问valuedouble,能够得到值。

若是String类型的,则valuestring中存储着值,能够訪问valuestring得到值。

若是多层嵌套的结构体要一层一层的进入,取值。
实例:截取部分代码

JSON结构体

多层嵌套:

{

"glossary": {

"title": "example glossary",

"GlossDiv": {

"title": "S",

"GlossList": {

"GlossEntry": {

"ID": "SGML",

"SortAs": "SGML",

"GlossTerm": "Standard Generalized Markup Language",

"Acronym": "SGML",

"Abbrev": "ISO 8879:1986",

"GlossDef": {

"para": "A meta-markup language, used to create markup languages such as DocBook.",

"GlossSeeAlso": ["GML", "XML"]

},

"GlossSee": "markup"

}

}

}

}

}

cJSON *glossary = cJSON_GetObjectItem(json,"glossary");

char *title = cJSON_GetObjectItem(glossary,"title")->valuestring;

printf("title===%s\n",title);

上传文件里面有JSON可以使用的所有功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: