c语⾔json数组查字符串,怎么⽤C语⾔获取JSON中的数据?
c语言如何创建字符串数组
⼦衿沉夜
⽤C语⾔获取JSON中的数据的⽅法是使⽤ CJSON。以下简单介绍⽤CJSON的思路及实现:1)创建json,从json中获取数据。#nclude
#include "cJSON.h"char * makeJson(){cJSON * pJsonRoot = NULL;pJsonRoot = cJSON_CreateObject();if(NULL == pJsonRoot){//error happend herereturn NULL;}cJSON_AddStringToObject(pJsonRoot, "hello", "hello
world");cJSON_AddNumberToObject(pJsonRoot, "number", 10010);cJSON_AddBoolToObject(pJsonRoot, "bool", 1);cJSON * pSubJson = NULL;pSubJson = cJSON_CreateObject();if(NULL == pSubJson){// create object faild,
exitcJSON_Delete(pJsonRoot);return NULL;}cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json
string");cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);char * p = cJSON_Print(pJsonRoot);// else use : // char * p = cJSON_PrintUnformatted(pJsonRoot);if(NULL == p){//convert json list to string faild, exit//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double freecJSON_Delete(pJsonRoot);return NULL;}//free(p);cJSON_Delete(pJsonRoot);return p;}void parseJson(char * pMsg)
{if(NULL == pMsg){return;}cJSON * pJson = cJSON_Parse(pMsg);if(NULL == pJson) {// parse faild, returnreturn ;}// get string from jsoncJSON * pSub = cJSON_GetObjectItem(pJson, "hello");if(NULL == pSub){//get object named "hello"
faild}printf("obj_1 : %s\n", pSub->valuestring);// get number from jsonpSub = cJSON_GetObjectItem(pJson,
"number");if(NULL == pSub){//get number from json faild}printf("obj_2 : %d\n", pSub->valueint);// get bool from jsonpSub = cJSON_GetObjectItem(pJson, "bool");if(NULL == pSub){// get bool from json faild} printf("obj_3 : %d\n", pSub-
>valueint);// get sub objectpSub = cJSON_GetObjectItem(pJson, "subobj");if(NULL == pSub){// get s
ub object faild}cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");if(NULL == pSubSub){// get object from subject object
faild}printf("sub_obj_1 : %s\n", pSubSub->valuestring);cJSON_Delete(pJson);}int main(){char * p = makeJson();if(NULL == p) {return 0;}printf("%s\n", p);parseJson(p);free(p); //这⾥不要忘记释放内存,cJSON_Print()函数或者
cJSON_PrintUnformatted()产⽣的内存,使⽤free(char *)进⾏释放return 0;}2)创建json数组和解析json数组//创建数组,数组值是另⼀个JSON的item,这⾥使⽤数字作为演⽰char * makeArray(int iSize){cJSON * root = cJSON_CreateArray(); if(NULL == root) {printf("create json array faild\n");return NULL;}int i = 0;for(i = 0; i < iSize; i++){cJSON_AddNumberToObject(root, "hehe", i);}char * out = cJSON_Print(root);cJSON_Delete(root);return out;}//解析刚刚的CJSON数组void parseArray(char * pJson)
{if(NULL == pJson){ return ;}cJSON * root = NULL;if((root = cJSON_Parse(pJson)) == NULL){return ;}int iSize =
cJSON_GetArraySize(root);for(int iCnt = 0; iCnt < iSize; iCnt++){cJSON * pSub = cJSON_GetArrayItem(root, iCnt);if(NULL == pSub){continue;}int iValue = pSub->valueint;printf("value
[%2d] : [%d]\n", iCnt, iValue);} cJSON_Delete(root);return;}有两种⽅法: ⼀是标准的输出输⼊⽅式 ⽐如新建⼀个磁盘⽂件c:\a.txt, 将键盘输⼊的⼀字符串写到⽂件中: FILE *ft; char
str[50]; ft=fopen("c:\\a.txt","w+"); printf("输⼊⼀个字符串:"); scanf("%s",str); fputs(str,ft); fclose(ft);//重新打开这个⽂件并读出字符串,显⽰在屏幕上 ft=fopen("c:\\a.txt","rt"); fgets(str,50,ft); fclose(ft); printf("%s",str); ⼆是低级输⼊输出⽅式 仍如上例: int hd; char str[50]; printf("输⼊⼀个字符
串:");scanf("%s",str);hd=open("c:\\a.txt",O_CREAT|O_TEXT|O_WRONLY);write(hd,str,strlen(str)); close(hd); //重新打开这个⽂件并读出字符串,显⽰在屏幕上。hd=open("c:\\a.txt",O_TEXT|O_RDONLY); read(hd,str,50); close(hd); printf("%s",str)。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论