cJSON 라 이브 러 리 (json 구축 및 json 문자열 해석) - c 언어
다운로드 주소:http://sourceforge.net/projects/cjson/
예 1 、 JSON 해석
예 2: JSON 데이터 생 성
char * json = "{ \"json\" : { \"id\":1, \"nodeId\":11, \"deviceId\":111, \"deviceName\":\"aaa\", \"ieee\":\"01212\", \"ep\":\"1111\", \"type\":\"bbb\" }}";
char * json1 = "{\"id\":1, \"nodeId\":11, \"deviceId\":111, \"deviceName\":\"aaa\"}";
cJSON * root;
cJSON * format;
int value_int;
char * value_string;
root = cJSON_Parse(json);
format = cJSON_GetObjectItem(root,"json");
value_int = cJSON_GetObjectItem(format,"nodeId")->valueint;
value_string = cJSON_GetObjectItem(format,"ieee")->valuestring;
printf( "%d
", value_int );
printf( "%s
", value_string );
cJSON_Delete(root);
root = cJSON_Parse(json1);
value_int = cJSON_GetObjectItem(root,"id")->valueint;
value_string = cJSON_GetObjectItem(root,"deviceName")->valuestring;
printf( "%d
", value_int );
printf( "%s
", value_string );
cJSON_Delete(root);
cJSON 라 이브 러 리
1. json 의 데이터 구조
c 언어 에서 json 데 이 터 는 링크 로 저 장 됩 니 다.
cJSON* pRoot = cJSON_CreateObject();
cJSON* pArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRoot, "students_info", pArray);
char* szOut = cJSON_Print(pRoot);
cJSON* pItem = cJSON_CreateObject();
cJSON_AddStringToObject(pItem, "name", "chenzhongjing");
cJSON_AddStringToObject(pItem, "sex", "male");
cJSON_AddNumberToObject(pItem, "age", 28);
cJSON_AddItemToArray(pArray, pItem);
pItem = cJSON_CreateObject();
cJSON_AddStringToObject(pItem, "name", "fengxuan");
cJSON_AddStringToObject(pItem, "sex", "male");
cJSON_AddNumberToObject(pItem, "age", 24);
cJSON_AddItemToArray(pArray, pItem);
pItem = cJSON_CreateObject();
cJSON_AddStringToObject(pItem, "name", "tuhui");
cJSON_AddStringToObject(pItem, "sex", "male");
cJSON_AddNumberToObject(pItem, "age", 22);
cJSON_AddItemToArray(pArray, pItem);
char* szJSON = cJSON_Print(pRoot);
cJSON_Delete(pRoot);
//free(szJSON);
pRoot = cJSON_Parse(szJSON);
pArray = cJSON_GetObjectItem(pRoot, "students_info");
if (NULL == pArray)
{
return -1;
}
int iCount = cJSON_GetArraySize(pArray);
for (int i = 0; i < iCount; ++i)
{
cJSON* pItem = cJSON_GetArrayItem(pArray, i);
if (NULL == pItem)
{
continue;
}
string strName = cJSON_GetObjectItem(pItem, "name")->valuestring;
string strSex = cJSON_GetObjectItem(pItem, "sex")->valuestring;
int iAge = cJSON_GetObjectItem(pItem, "age")->valueint;
}
cJSON_Delete(pRoot);
free(szJSON);
3. cJSON 사용
typedef struct cJSON {
struct cJSON *next,*prev;//
struct cJSON *child;//
int type;// ,
char *valuestring;//
int valueint; //
double valuedouble;// cJSON_Number
char *string;// The item's name string, if this item is the child of, or is in the list of subitems of an object.
} cJSON;
1. 문자열 을 json 구조 체 로 해석
1): 문자열 을 json 구조 체 로 해석 합 니 다.
cJSON *root = cJSON_Parse(my_json_string);
2): 어떤 원소 가 져 오기
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3): json 구조 체 를 문자열 로 변환 하기
char *rendered=cJSON_Print(root);
4): 삭제
cJSON_Delete(root);
2: json 구조 체 구축
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type", "rect");
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate", 24)
out =cJSON_Print(root);
printf("%s",out);
cJSON_Delete(root);
free(out);
다음으로 전송:https://www.cnblogs.com/young525/p/5873783.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.