cJSON库的使用
cJSON是什么? cJSON是C语言中的一个JSON编解码器。非常轻量级,代码的可读性也很好,适合C语言项目进行学习。
1.下载和安装
sourceforge.net/projects/cj…
下载完成后解压缩,只需要将cJSON.c和cJSON.h文件和自己的工程一起编译即可。
2.JSON语法
2.1 JSON三种语法:
键/值对 key/value, 用冒号分割。 比如"name":"Jack"
- JSON对象写在花括号中,可以包含多个键/值对。比如{"width":1920,"height":1080}
- JSON数组写在方括号中,数组成员可以是对象、值,也可以是数组(只要有意义)。比如{"week:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]}
- JSON的值可以是:数字Number(整数或浮点数)、字符串String(在双引号中)、逻辑值(Ture或False)、数组Array(在方括号中)、对象Object(在花括号中)、NULL
2.2 JSON两种结构:复杂的数据结构都是由对象和数据组成的
-
对象:对象在js中表示为“{}”括起来的内容,数据结构为{key:value...}的键值对的结构,在面向对象的语言中,对象直接调用对象的属性来回去属性的值。key为对象的属性,value为对应的属性值,取值方法为对象.key获取属性值
-
数组:数组在js中是中括号“[]”括起来的内容,数据结构为["Sunday","Monday","Tuesday",...],取值方式利用数据下标索引
3.Linux下字符串解析小实验
和风天气数据解析
JSON字符串:
{
"HeWeather6": [{
"basic": {
"cid": "CN101010700",
"location": "昌平",
"parent_city": "北京",
"admin_area": "北京",
"cnty": "中国",
"lat": "40.21808624",
"lon": "116.23590851",
"tz": "+8.00"
},
"update": {
"loc": "2018-11-21 21:45",
"utc": "2018-11-21 13:45"
},
"status": "ok",
"daily_forecast": [{
"cond_code_d": "100",
"cond_code_n": "100",
"cond_txt_d": "晴",
"cond_txt_n": "晴",
"date": "2018-11-21",
"hum": "21",
"mr": "16:02",
"ms": "04:27",
"pcpn": "0.0",
"pop": "0",
"pres": "1030",
"sr": "07:08",
"ss": "16:53",
"tmp_max": "9",
"tmp_min": "-3",
"uv_index": "5",
"vis": "10",
"wind_deg": "323",
"wind_dir": "西北风",
"wind_sc": "1-2",
"wind_spd": "4"
}, {
"cond_code_d": "100",
"cond_code_n": "101",
"cond_txt_d": "晴",
"cond_txt_n": "多云",
"date": "2018-11-22",
"hum": "21",
"mr": "16:36",
"ms": "05:33",
"pcpn": "0.0",
"pop": "0",
"pres": "1030",
"sr": "07:09",
"ss": "16:52",
"tmp_max": "8",
"tmp_min": "-4",
"uv_index": "3",
"vis": "20",
"wind_deg": "35",
"wind_dir": "东北风",
"wind_sc": "1-2",
"wind_spd": "5"
}, {
"cond_code_d": "101",
"cond_code_n": "100",
"cond_txt_d": "多云",
"cond_txt_n": "晴",
"date": "2018-11-23",
"hum": "23",
"mr": "17:15",
"ms": "06:41",
"pcpn": "0.0",
"pop": "16",
"pres": "1024",
"sr": "07:10",
"ss": "16:52",
"tmp_max": "7",
"tmp_min": "-2",
"uv_index": "2",
"vis": "20",
"wind_deg": "305",
"wind_dir": "西北风",
"wind_sc": "1-2",
"wind_spd": "3"
}]
}]
}
解析函数:
void parse_heweather(void)
{
char heweather_str[] = "{"HeWeather6":[{"basic":{"cid":"CN101010700","location":"昌平","parent_city":"北京","admin_area":"北京","cnty":"中国","lat":"40.21808624","lon":"116.23590851","tz":"+8.00"},"update":{"loc":"2018-11-21 21:45","utc":"2018-11-21 13:45"},"status":"ok","daily_forecast":[{"cond_code_d":"100","cond_code_n":"100","cond_txt_d":"晴","cond_txt_n":"晴","date":"2018-11-21","hum":"21","mr":"16:02","ms":"04:27","pcpn":"0.0","pop":"0","pres":"1030","sr":"07:08","ss":"16:53","tmp_max":"9","tmp_min":"-3","uv_index":"5","vis":"10","wind_deg":"323","wind_dir":"西北风","wind_sc":"1-2","wind_spd":"4"},{"cond_code_d":"100","cond_code_n":"101","cond_txt_d":"晴","cond_txt_n":"多云","date":"2018-11-22","hum":"21","mr":"16:36","ms":"05:33","pcpn":"0.0","pop":"0","pres":"1030","sr":"07:09","ss":"16:52","tmp_max":"8","tmp_min":"-4","uv_index":"3","vis":"20","wind_deg":"35","wind_dir":"东北风","wind_sc":"1-2","wind_spd":"5"},{"cond_code_d":"101","cond_code_n":"100","cond_txt_d":"多云","cond_txt_n":"晴","date":"2018-11-23","hum":"23","mr":"17:15","ms":"06:41","pcpn":"0.0","pop":"16","pres":"1024","sr":"07:10","ss":"16:52","tmp_max":"7","tmp_min":"-2","uv_index":"2","vis":"20","wind_deg":"305","wind_dir":"西北风","wind_sc":"1-2","wind_spd":"3"}]}]}";
cJSON *root;
cJSON *results;
cJSON *basic_json, *update_json, *forecast_json;
cJSON *daily_json;
int i = 0;
char *basic_tmp, *update_tmp, *status_tmp, *weather_tmp;
root = cJSON_Parse(heweather_str);
if(root)
{
results = cJSON_GetObjectItem(root, "HeWeather6"); //HeWeather键对应的值,是一个数组
results = cJSON_GetArrayItem(results,0);
if(results)
{
basic_json = cJSON_GetObjectItem(results, "basic");
if(basic_json)
{
basic_tmp = cJSON_GetObjectItem(basic_json, "cid") -> valuestring;
printf("城市ID:%s\n",basic_tmp);
basic_tmp = cJSON_GetObjectItem(basic_json, "location") -> valuestring;
printf("县级市:%s\n",basic_tmp);
basic_tmp = cJSON_GetObjectItem(basic_json, "parent_city") -> valuestring;
printf("地级市:%s\n",basic_tmp);
basic_tmp = cJSON_GetObjectItem(basic_json, "admin_area") -> valuestring;
printf("所属省:%s\n",basic_tmp);
basic_tmp = cJSON_GetObjectItem(basic_json, "lat") -> valuestring;
printf("纬度:%s\n",basic_tmp);
basic_tmp = cJSON_GetObjectItem(basic_json, "lon") -> valuestring;
printf("经度:%s\n\n",basic_tmp);
}
update_json = cJSON_GetObjectItem(results, "update");
if(update_json)
{
update_tmp = cJSON_GetObjectItem(update_json, "loc") -> valuestring;
printf("更新时间:%s(所在地时间)\n", update_tmp);
update_tmp = cJSON_GetObjectItem(update_json, "utc") -> valuestring;
printf("更新时间:%s(世界时间)\n\n", update_tmp);
}
status_tmp = cJSON_GetObjectItem(results, "status") -> valuestring;
printf("解析状态:%s\n\n", status_tmp);
daily_json = cJSON_GetObjectItem(results, "daily_forecast");
if(daily_json)
{
for(i = 0; i < 3; i++)
{
forecast_json = cJSON_GetArrayItem(daily_json, i);
weather_tmp = cJSON_GetObjectItem(forecast_json, "date") -> valuestring;
printf("日期:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "cond_txt_d") -> valuestring;
printf("白天天气:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "cond_txt_n") -> valuestring;
printf("晚上天气:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "tmp_max") -> valuestring;
printf("最高温度:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "tmp_min") -> valuestring;
printf("最低温度:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_deg") -> valuestring;
printf("风向角度:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_dir") -> valuestring;
printf("风向:%s\r\n", weather_tmp);
weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_sc") -> valuestring;
printf("风力:%s\r\n\n", weather_tmp);
}
}
}
}
cJSON_Delete(root);
cJSON_Delete(results);
cJSON_Delete(basic_json);
cJSON_Delete(update_json);
cJSON_Delete(forecast_json);
cJSON_Delete(daily_json);
}
在线JSON格式校验网站:www.bejson.com/
引用:
1.www.cnblogs.com/txwtech/p/1… cJSON库的使用
2.【嵌入式物联网-linux下字符串解析器项目,数据交换格式JSON的基本语法讲解,开源库cJSON介绍】 www.bilibili.com/video/BV1hG…