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可以使用的所有功能。
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可以使用的所有功能。
相关文章推荐
- js实现自定义滚动条
- 浅谈 JavaScript DDoS 攻击原理与防御
- js submit方法与onsubmit事件
- JavaScript在IE6,IE7下报错'expected identifier, string or number'
- 原生js显示分页效果
- ExtJS 的一些使用总结(适用于3.x版本)——第二弹
- EL表达式中fn函数详解
- 在动态web工程中jsp页面的EL表达式没有效果
- backbone.js快速入门(七)--模型2
- An Introduction to the Raphael JS Library
- 002-使用javascript
- COJS 1752. [BOI2007]摩基亚Mokia
- 比较net.sf.json和org.json
- js代码触发onchange事件
- js_事件流
- js浮点数精确计算(加、减、乘、除)
- 在光标选中位置插入文字js
- js如何查看元素类型
- js正则表达式test()和exec()用法实例
- js正则表达式实例(汇总)