四:初次尝试
- #include "cJSON.h"
- #include "stdio.h"
-
-
-
-
- int main ()
- {
- const char* data = { "{"results":0000,"xingbie":"nan"}" };
- //1.创建cJSON指针
- cJSON * ceshi = NULL;
- //2.解析整段数据, 返回指针
-
- ceshi = cJSON_Parse (data);
- if (!ceshi)
- {
- printf ("error:%s",cJSON_GetErrorPtr());
- }
- else
- {
- printf ("解析成功");
- //3.根据键值对的名称从链表中取出对应的值,返回该键值对的地址
- cJSON* jsonXingbie = cJSON_GetObjectItem (ceshi, "xingbie");
- //4.根据类型提取相关值
- if (jsonXingbie->type == cJSON_String)
- {
- printf ("%s", jsonXingbie->valuestring);
- }
-
- }
- //5.释放
- cJSON_Delete (ceshi);
- return 0;
- }
复制代码
|