cJSON 라 이브 러 리 (json 구축 및 json 문자열 해석) - c 언어

프로젝트 에 먼저 cJSON 라 이브 러 리 가 있어 야 합 니 다. 두 파일 은 각각 cJSON. c 와 cJSON. h 입 니 다.
다운로드 주소: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

좋은 웹페이지 즐겨찾기