json格式
#define cJSON_Invalid ( 0 )
#define cJSON_False ( 1 << 0 )
#define cJSON_True ( 1 << 1 )
#define cJSON_NULL ( 1 << 2 )
#define cJSON_Number ( 1 << 3 )
#define cJSON_String ( 1 << 4 )
#define cJSON_Array ( 1 << 5 )
#define cJSON_Object ( 1 << 6 )
#define cJSON_Raw ( 1 << 7 )
json數組
中括號[整形,字符串,布爾類型,json數組,josn對象]
[123,123.4,true, false, [12,34,56,“hello”,“world”]]
細節 :最後一個元素是沒有逗號的
json 對象
{ }中是一些鍵值對
{“name”:“ZhangSan”,
“name”:“li4”}
key值:必須是字符串,且不重複
value值:可以使json對象,json數組,布爾,整形,字符串
json數組對象
json數組+json對象
{“name”:“ZhangSan”,
“name”:“li4”,
“張三”:{
“別名”:“老王”,
“性別”:“男”,
“孩子”:"[“小紅”,“小黑”,“小白”]"
}
}
Cjson
下載cjson-master.zip包,解壓命令 unzipt CJSON-master.zip
cJson對象
typedef struct cJSON
{
struct cJSON * next;
struct cJSON * prev;
struct cJSON * child;
int type;
char * valuestring;
int valueint;
double valuedouble;
char * string;
} cJSON;
CJSON_PUBLIC ( cJSON * ) cJSON_CreateObject ( void )
{
cJSON * item = cJSON_New_Item ( & global_hooks) ;
if ( item) {
item- > type = cJSON_Object;
}
return item;
}
CJSON_PUBLIC ( void ) cJSON_AddItemToObject (
cJSON * object,
const char * string,
cJSON * item
) ;
cJSON * cJSON_CreateNumber ( double num) ;
cJSON * cJSON_CreateString ( const char * string) ;
寫入json數據
{
"奔馳" :{
"factory" : "北京奔馳" ,
"last" : 31 ,
"price" : 83 ,
"sell" : 49 ,
"other" : [ 123 ,
true ,
"hello" ,
{ "梅賽德斯奔馳" : "The best or nothing" }
]
}
}
int main ( )
{
cJSON * obj = cJSON_CreateObject ( ) ;
cJSON * subobj= cJson_CreateObject ( ) ;
cJSON_AddItemToObject ( subobj, "factory" , cJSON_CreateString ( "北京奔馳" ) ) ;
cJSON_AddItemToObject ( subobj, "last" , cJSON_CreateNumber ( 31 ) ) ;
cJSON_AddItemToObject ( subobj, "price" , cJSON_CreateNUmer ( 83 ) ) ;
cJSON_AddItemToObject ( subobj, "sell" , cJSON_CreateNUmer ( 49 ) ) ;
cJSON * array = cJSON_CreateArray ( ) ;
cJSON_AddItemToArray ( array, cJSON_CreateNumber ( 123 ) ) ;
cJSON_AddItemToArray ( array, cJSON_CreateBool ( 1 ) ) ;
cJSON_AddItemToArray ( array, cJSON_CreateString ( "hello" ) ) ;
cJSON * subsub = cJSON_CreateObject ( ) ;
cJSON_AddItemToObject ( subsub, "梅賽德斯奔馳" , cJSON_CreateString ( "The best or Nothing" ) ) ;
cJSON_AddItemToArray ( array, subsub) ;
cJSON_AddItemToObject ( subobj, "other" , array) ;
cJson_addItemtoObj ( "obj" , "奔馳" ,subobj) ;
char * data = cJSON_Print ( obj) ;
FILE * fp = fopen ( "car.json" , "w" ) ;
fwrite ( data, sizeof ( char) , strlen ( data) + 1 , fp) ;
fclose ( fp) ;
return 0 ;
}
錯誤 :在函數‘print_number’中:cJSON.c:對‘floor’未定義的引用
將函數對應的庫加入 編譯時增加 -lm ,math庫
讀json數據
( cJSON * ) cJSON_Parse (
const char * value;
) ;
( cJSON * ) cJSON_GetObjectItem (
const cJSON * const object,
const char * const string
) ;
typedef struct cJSON
{
struct cJSON * next;
struct cJSON * prev;
struct cJSON * child;
int type;
char * valuestring;
int valueint;
double valuedouble;
char * string;
} cJSON;
int cJSON_GetArraySize ( cJSON * array) ;
cJSON * cJSON_GetArrayItem ( cJSON * array, int index) ;
cJSON * cJSON_HasObjectItem ( cJSON * object, const char* string) ;
json = cJSON_Parse ( topic_info- > payload) ;
json_id = cJSON_GetObjectItem ( json, id) ;
if ( json_id. type == cJSON_String)
{
HAL_Printf ( "id:%s\r\n" , json_id- > valuestring) ;
}
json_params = cJSON_GetObjectItem ( json, "params" ) ;
if ( json_params)
{
json_led = cJSON_GetObjectItem ( json_params, "led" ) ;
if ( json_led- > type == cJSON_Number)
{
HAL_Printf ( "LED:%d\r\n" , json_led- > valueint) ;
}
}