본문 바로가기
프로그래밍/C,C++

cJSON API 저장

by Planetis 2015. 7. 19.

* 원문 : http://wiki.openpicus.com/index.php/JSON_Parser


JSON APIs

The main data type to use JSON library is struct cJSON

The following tables describe JSON related APIs 

Parsing

FunctionDescription
 cJSON *cJSON_Parse(const char*value)This function parses a JSON string

const char *value - pointer to a cJSON string to be parsed

Returns cJSON *- pointer to the cJSON parsed

NOTE Call cJSON_Delete(cJSON *c) function when the JSON is not longer needed to free the memory.

 void   cJSON_Delete(cJSON *c)This function deletes a JSON to free memory.

cJSON *c – pointer to the cJSON to be deleted

 char  *cJSON_Print(cJSON*item)This function prints a JSON in a string in a formatted style.

cJSON * item - Pointer to a cJSON to be printed

Returns char * - pointer to the string

NOTE Call free(char *) function when the string is no longer needed to free memory.

 char *cJSON_PrintUnformatted(cJSON*item)This function prints a JSON in a string in a unformatted style.

cJSON * item - Pointer to a cJSON to be printed

Returns char * - pointer to the string

NOTE Call free(char *) function when the string is no longer needed to free memory.

 const char*cJSON_GetErrorPtr(void)This function returns a pointer to a string when an error was encountered during the parsing.

Returns const char* - is the pointer to a error string

NOTE Looking some chars back can give a better help to find the error encountered.


Getting and item

FunctionDescription
 cJSON*cJSON_GetObjectItem(cJSON*object,const char *string)This function returns a pointer to an item in a JSON.

cJSON *object – pointer to a cJSON. const char *string – pointer to the item's name.

Returns cJSON * - pointer to the item found

 intcJSON_GetArraySize(cJSON*array)This function returns the size of an array in a JSON

cJSON * array – pointer to an array.

Returns int item - size of the array

 cJSON*cJSON_GetArrayItem(cJSON*array,int item)This function returns a pointer to an item in an array.

cJSON * array – pointer to an array. int item – number of the item in the array

Returns cJSON * - pointer to the item in the array.


Deleting/Detaching an item

FunctionDescription
 voidcJSON_DeleteItemFromObject(cJSON*object,const char *string)This function deletes an item from JSON

cJSON * object – pointer to the cJSON const char *string – pointer to the item to be deleted

 cJSON*cJSON_DetachItemFromObject(cJSON*object,const char *string)This function detaches an item from JSON.

cJSON * object – pointer to the cJSON. const char *string – pointer to the item to be detached.

Returns cJSON * - pointer to the item detached.


Creating and adding a JSON object/array

NameFunction Description
 cJSON*cJSON_CreateObject()This function creates a JSON

Returns cJSON * - pointer to the cJSON created

 cJSON *cJSON_CreateArray()This function creates an array.

Returns cJSON * - pointer to the array created



320x100

'프로그래밍 > C,C++' 카테고리의 다른 글

랜덤 함수  (0) 2015.01.27
switch문 case내 변수 초기화시 에러  (0) 2015.01.27
#pragma 지시자 ( once, pack, warning )  (0) 2015.01.20
inline 함수  (0) 2015.01.20
상수화 const  (0) 2015.01.20

댓글