本文目錄一覽:
怎麼用C語言獲取JSON中的數據?
用C語言獲取JSON中的數據的方法是使用 CJSON。
以下簡單介紹用CJSON的思路及實現:
1)創建json,從json中獲取數據。
#nclude stdio.h
#include “cJSON.h”
char * makeJson()
{
cJSON * pJsonRoot = NULL;
pJsonRoot = cJSON_CreateObject();
if(NULL == pJsonRoot)
{
//error happend here
return 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, exit
cJSON_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 free
cJSON_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, return
return ;
}
// get string from json
cJSON * pSub = cJSON_GetObjectItem(pJson, “hello”);
if(NULL == pSub)
{
//get object named “hello” faild
}
printf(“obj_1 : %s\n”, pSub-valuestring);
// get number from json
pSub = cJSON_GetObjectItem(pJson, “number”);
if(NULL == pSub)
{
//get number from json faild
}
printf(“obj_2 : %d\n”, pSub-valueint);
// get bool from json
pSub = cJSON_GetObjectItem(pJson, “bool”);
if(NULL == pSub)
{
// get bool from json faild
}
printf(“obj_3 : %d\n”, pSub-valueint);
// get sub object
pSub = cJSON_GetObjectItem(pJson, “subobj”);
if(NULL == pSub)
{
// get sub 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)。
【Lua】cjson解析null
最近遇到一個問題,在lua中使用cjson解析json中數據為null時,解析出來是一個userdata。如圖:
我們需要判斷這個值使用cjson.null。
具體的可以看看 文檔 很詳細。
如何用ESP8266構建一個JSON樹,並用JSON函數解析JSON數據
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。它基於ECMAScript的一個子集。 JSON採用完全
獨立於語言的文本格式,但是也使用了類似於C語言家族的**慣(包括C、C++、C#、Java、JavaScript、Perl、Python等)
。這些特性使JSON成為理想的數據交換語言。 易於人閱讀和編寫,同時也易於機器解析和生成(一般用於提升網路傳輸速
率)。
JSON 語法規則
JSON 語法是 JavaScript 對象表示語法的子集。
數據在鍵值對中
數據由逗號分隔
花括弧保存對象
方括弧保存數組
JSON 名稱/值對
JSON 數據的書寫格式是:名稱/值對。
名稱/值對組合中的名稱寫在前面(在雙引號中),值對寫在後面(同樣在雙引號中),中間用冒號隔開:
“firstName”:”John”
這很容易理解,等價於這條 JavaScript 語句:
firstName=”John”
JSON 值
JSON 值可以是:
數字(整數或浮點數)
字元串(在雙引號中)
邏輯值(true 或 false)
數組(在方括弧中)
對象(在花括弧中)
null
JSON實戰應用篇
由於項目需要,產品一直使用的ESP8266,為了更好的適應實時性控制項目的需求,重新設計了伺服器的架構,使用了基
於消息訂閱和**的MQTT架構,為了滿足伺服器同事更好的解析數據的需求,我很無奈的選擇了使用JSON數據格式和服務
器交互(其實我還是很喜歡01的,可惜搞**機的更喜歡直接調用現成的API),所以我的WIFI必須支撐JSON,幸好WIFI集
成了JSON API,可惜文檔特別不好理解,搞了很久才明白如何構建一個JSON樹(我都打算直接在MCU裡面用CJSON來實現了
),要構建的JSON樹也挺簡單的「device_info」:{ “sbtm”:”7090150****0882****34″, “k”:”0001″, “v”:”0″ } sbtm是ID
屬性名,K功能碼屬性名,v命令熟悉值(冒號後面的屬性值)。具體代碼如下:
/******************************************************************************
* FunctionName : device_get
* Description : set up the device information parmer as a JSON format
* Parameters : js_ctx — A pointer to a JSON set up
* Returns : result
*******************************************************************************/
LOCAL int ICACHE_FLASH_ATTR
device_get(struct jsontree_context *js_ctx)
{
const char *path = jsontree_path_name(js_ctx, js_ctx-depth – 1);
if (os_strncmp(path, “sbtm”, 4) == 0)
{
jsontree_write_string(js_ctx, “7090150****0882****34”);
}
else if (os_strncmp(path, “k”,1) == 0)
{
jsontree_write_string(js_ctx, “0x0002”);
}
else if (os_strncmp(path, “v”,1) == 0)
{
jsontree_write_string(js_ctx,” 0″);
}
return 0;
}
LOCAL struct jsontree_callback device_callback =
JSONTREE_CALLBACK(device_get, NULL);
JSONTREE_OBJECT(device_tree,JSONTREE_PAIR(“sbtm”,device_callback),JSONTREE_PAIR
(“k”,device_callback),JSONTREE_PAIR(“v”,device_callback));
JSONTREE_OBJECT(deviceinfo_tree,JSONTREE_PAIR(“device_info”,device_tree));
其中宏定義JSONTREE_OBJECT是生成一個JSON數的對象,第一個參數是該對象的名稱(device_tree),JSONTREE_PAIR是生
成一個鍵值對的宏。
JSONTREE_CALLBACL是生成一個回調指針的宏,該宏有兩個參數,第一個參數是設置讀取JSON樹的值的函數,這裡為
device_get函數,第二個參數是設置寫入JSON樹的值的函數,這裡沒有用到,為NULL。
device_get是讀取JSON樹的值的函數。其中用os_strncnp進行JSON屬性名的判斷,這裡示例是:如果屬性名為”sbtm”,則
獲取的值為”7090150****0882****34″。通過json_ws_send((struct jsontree_value *)deviceinfo_tree, “device_info”,
buf); 函數可以獲得JSON樹的內容,最後通過MQTT_Publish(mqttClient, “test/json”,buf ,os_strlen(buf), 0,0);將
JSON發送到MQTT測試伺服器上面
接下來就是如何解析伺服器下發的JSON樹(對於JSON樹如何解析,8266都沒資料介紹,把百度谷歌找了一遍也沒找到,最後還是自己搞定了,希望後來者少走些彎路)
首先需要初始化一顆JSON樹並將你需要解析的JSON的JSON對象deviceinfo_tree填入到jsontree_setup第二個參數,最後調
用device_parse解析就行。
struct jsontree_context js;
jsontree_setup(js, (struct jsontree_value *)deviceinfo_tree, json_putchar);
json_parse(js, buffer);
LOCAL struct jsontree_callback device_callback =
JSONTREE_CALLBACK(device_get,device_parse);
/******************************************************************************
* FunctionName : device_parse
* Description : parse the device status parmer as a JSON format
* Parameters : js_ctx — A pointer to a JSON set up
* parser — A pointer to a JSON parser state
* Returns : result
*******************************************************************************/
LOCAL int ICACHE_FLASH_ATTR
device_parse(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
int type;
uint8_t status,cmd;
while ((type = jsonparse_next(parser)) != 0)
{
if (type == JSON_TYPE_PAIR_NAME)
{
if (jsonparse_strcmp_value(parser, “k”) == 0)
{
jsonparse_next(parser);
jsonparse_next(parser);
cmd = jsonparse_get_value_as_int(parser);
os_printf(“cmd=%d”,cmd);
}
else if(jsonparse_strcmp_value(parser, “v”) == 0)
{
uint8 status;
jsonparse_next(parser);
jsonparse_next(parser);
status = jsonparse_get_value_as_int(parser);
os_printf(“status=%d”,status);
}
if(cmd== RO_STATE_CMD)
{
}
else if(cmd== POWR_CMD)
{
if(status==0x01)
{
os_printf(“power off”);
}
else
{
os_printf(“power on”);
}
}
else if(cmd== FILTER_CMD)
{
if(status==0x01)
{
os_printf(“filter on”);
}
else
{
os_printf(“filter off”);
}
}
}
}
return 0;
}
伺服器下發的JSON命令解析成功~·`~~~~
原創文章,作者:XDAC,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/150159.html