网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

josn常用函数c语言 join函数c语言

JSON解析器json-c

JSON-C实现了一个引用计数对象模型,它允许您轻松地使用C语言来构建JSON对象,将它们输出为JSON格式的字符串,并将JSON格式字符串解析回JSON对象的C语言表示形式。它的目标是符合 RFC 7159 标准。

创新互联建站专注于网站建设、做网站、网页设计、网站制作、网站开发。公司秉持“客户至上,用心服务”的宗旨,从客户的利益和观点出发,让客户在网络营销中找到自己的驻足之地。尊重和关怀每一位客户,用严谨的态度对待客户,用专业的服务创造价值,成为客户值得信赖的朋友,为客户解除后顾之忧。

使用automake的编译过程如下:

使用cmake编译的过程如下:

cmake可选的几个编译选项为:

要使用json-c,最简单的方式是包含json.h头文件即可,或者最好是下列更具体的头文件之一:

详细且全面的API介绍文档:

JSON-C支持的JSON对象类型有7种:

下面系列函数用于创建一个JSON对象:

给JSON对象增加字段(不会增加引用计数):

删除json对象的指定字段,被删除的对象引用计数减去1,如果这个val没有更多的所有者,这个key对应的val被free,否则这个val的引用保存在内存中:

增加一个元素到json数组的末尾,obj引用计数不会增加,增加字段的方式更加紧凑;如果需要获取val的引用,需要用json_object_get()来传递该对象:

替换json数组中的值:

json数组的排序,这里需要自己写排序函数:

获取json对象的长度,依据字段的数目:

获取json对象的哈希表:

获取对象的数组列表:

获取json的类型:

获取json数组对象的长度:

获取json对象的bool值,int和double对象是0转换为FALSE,否则返回TRUE;非0长度的字符串返回TRUE;其他对象非空的话,返回TRUE:

获取json对象的长度,如果参数不是string类型的json,返回0:

按照索引获取json数组的对象:

转换json对象到c字符串格式:

获取JSON中指定类型的数值:

将字符串转换为json对象:

以下两个函数配合使用,前者获取该对象指针的所有权,引用计数加1,如果对象已经被释放,返回NULL;后者引用计数减1,如果对象已经被释放,返回1:

类型判断:

json_util.h提供了有关文件读写操作的函数,这个文件的内容是json格式的:

怎么用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)。

C#json有哪些常用类库

据结构应该是对应的,这里的例子可能忽略了部分属性和数据类型,仅供参考:

//Json数据的对象结构

public class MyJson

{

public ListMyTreeNodeBean beanList

}

//树形结构中的对象结构

public class MyBean

{

public string id

public string title

public string note

public MyImage image

public string maxid

public string fold

public string putright

public ListMyTreeNodeBean children

public MyBean()

{

}

}

//对象里面Image的结构

public class MyImage

{

public string url

public string border

public string height

public string width

}

2.因为json是树形结构的数据,所以需要递归遍历去寻找对应ID的对象

//这个类负责按照ID寻找对象

public class MyHelper

{

//FindById函数的重载函数,用来第一次调用

public static MyBean FindById(string id, string jsonString)

{

return FindById(id, jsonString, null);

}

//FindById递归寻找函数

public static MyBean FindById(string id, string jsonString, ListMyBean beanList)

{

if (beanList == null) {

//第一次调用的时候,寻找的列表是最高层的根底下的对象列表

//将json数据转换成C#对应类型(用了Newtonsoft.Json)

MyJson json = JavaScriptConvert.DeserializeObjectMyJson(jsonString);

beanList = json.beanList;

}

//遍历对象列表,寻找对应ID的对象

MyBean returnBean = null;

foreach (MyBean bean in beanList) {

if (bean.id == id) {

//找到了

returnBean = bean;

} else {

//递归寻找:

//如果不是,就寻找此对象的children列表

returnBean = FindById(id, jsonString, bean.children);

}

//如果找到,就跳出寻找

if (returnBean != null) {

break;

}

}

return returnBean;

}

}

3.使用实例:

//假设json的字符串在变量jsonString中

MyBean bean = MyHelper.FindById("33", jsonString);

if (bean != null) {

//使用查找出来的bean

}


网站名称:josn常用函数c语言 join函数c语言
网页链接:http://bjjierui.cn/article/ddioogo.html

其他资讯