JSON-C结构简介、使用

官方站点介绍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 LanguageStandard 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 指针来訪问,进入下一层。

2type一共同拥有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类型。则valueintvaluedouble中存储着值。若你期望的是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可以使用的所有功能。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值