当前位置:首页 » 编程语言 » c语言实现json
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现json

发布时间: 2022-09-18 22:02:08

1. 用c语言解析JSON数据

http://www.json.org/
列出了一堆C语言的JSON库。

C:
JSON_checker.
YAJL.
js0n.
LibU.
json-c.
json-parser.
jsonsl.
WJElement.
M's JSON parser.
cJSON.
Jansson.
jsmn.
cson.
parson.
ujson4c.
nxjson.
frozen.

2. C/C++实现WebService服务提供JSON数据的接口

1、C++可以实现webservice,这是毋庸置疑的.axis2本质是运行在tomcat下的一个servlet,分java版本,和C语言版本.官方网站为:http://axis.apache.org/,首页上写着:
The well known Apache Axis, and the the second generation of it, the Apache Axis2, are two Web Service containers that helps users to create, deploy, and run Web Services.Axis2 is avaialble in both Java as well as C, languages and details about each version can be found below. 大概意思就是这东西分java版本和C版本,可以方便用户创建,部署,运行web service.而C++完全是兼容C的.
2、需要服务器,要实现某个服务吧,至于怎样为其他平台服务,主要是监听端口实现解析http协议.js不需要拼串成XML,服务器才要拼串,JS是运行在客户端的,客户端也不是通过SOAP与服务端进行通讯的,而是根据需要调用的服务的WSDL,提供对应参数,客户端与服务端的通讯是用http协议的,而通讯方式根据是GET还是POST把相关参数放到HTTP头或者体中.而web service之间的通讯才可能用得到SOAP.
3、PHP调用web service是非常简单的,貌似有个函数通过SOAP调用.C++编写的web service肯定有WSDL,可以根据WSDL描述的端口参数,来调用.

3. C语言读取多行json文件数据 用哪种库比较好, 具体怎么操作

有的是 下面是超市 请自选 JSON_checker. YAJL. js0n. LibU. json-c. json-parser. jsonsl. WJElement. M's JSON parser. cJSON. Jansson. jsmn. cson. parson. ujson4c. nxjson.

4. 怎么用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 coremp, 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 ", pSub->valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, "number");

if(NULL == pSub)

{

//get number from json faild

}

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

// get bool from json

pSub = cJSON_GetObjectItem(pJson, "bool");

if(NULL == pSub)

{

// get bool from json faild

}

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

cJSON_Delete(pJson);

}


int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

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

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] ", iCnt, iValue);

}

cJSON_Delete(root);

return;

}

5. json格式怎么打开

打开json格式的文件的具体操作步骤如下:

操作设备:联想拯救者Y9000。

操作系统:Win10专业版。

操作软件:记事本。

1、首先在电脑的桌面上使用鼠标右键单击要进行打开的“json”格式的文件,然后在弹出的选项框内点击“打开方式”选项。

json格式的特色:

它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯。 这些特性使JSON成为理想的数据交换语言。

6. c语言 解析json字符串

你好,你用json-c库,编译通过了吗?我是在ubuntu里使用json-c库,但是无法编译通过,报错 undefined reference to 'json_tokener_parse',类似的函数没定义的错误,你是怎么调用的json-c库?请教一下,谢谢!

7. 如何使用c语言获取文件中的json数据

直接文件操作就行了。fopen,然后直接读出文件中的json数据,保存到一个数组里面就行了

8. C语言可以直接把数据库查询出来的数据变成json格式吗,有直接的库,可以调用吗

有的是 下面是超市 请自选

JSON_checker.
YAJL.
js0n.
LibU.
json-c.
json-parser.
jsonsl.
WJElement.
M's JSON parser.
cJSON.
Jansson.
jsmn.
cson.
parson.
ujson4c.
nxjson.

9. 如何用c实现http post json

http是基于Socket通信的一种通信规约,post是http规约的一种功能,json是常用于字符串解释型编程语言及一些脚本上用的对象格式。