標籤:gcc json 結構 object xml
官方網站介紹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所有可能用到的函數