首页 > 编程知识 正文

cjson函数解析(cjson_print函数)

时间:2023-12-24 12:05:23 阅读:320156 作者:ZCSA

本文目录一览:

怎么用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 : %sn", pSub-valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, "number");

if(NULL == pSub)

{

//get number from json faild

}

printf("obj_2 : %dn", pSub-valueint);

// get bool from json

pSub = cJSON_GetObjectItem(pJson, "bool");

if(NULL == pSub)

{

// get bool from json faild

}

printf("obj_3 : %dn", 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 : %sn", pSubSub-valuestring);

cJSON_Delete(pJson);

}

int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

printf("%sn", 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 faildn");

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命令解析成功~·`~~~~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。