The article directories

    • CJSON library download
    • JSON string parsing containing only key-value pairs
    • JSON string parsing containing arrays
    • Beijing time JSON data parsing
    • Heart knows weather JSON data parsing
    • Analysis of wind weather data
    • Selected historical articles:

CJSON library download

#include “cjson. h” #include” cjson. h” #include “cjson. h” #include” cjson. h” #include “cjson. h” #include” cjson. h”

  • Library source code download address: cJSON download
  • JSON Official website: JSON

JSON string parsing containing only key-value pairs

JSON string:

{"name": "Andy", // key pair 1 "age": 20 // key pair 2}Copy the code

[External link image transfer failed, the source station may have anti-theft mechanism, it is recommended to save the image directly upload (IMG-C5CDYYHF-1569933943620)(www.json.org/object.gif)]

The JSON object has only two key-value pairs. The key name corresponds to the string Andy and the key age corresponds to the number 20.

void Parse_Str1(void) { char str1[] = "{\"name\":\"Andy\",\"age\":20}"; cJSON *str1_json, *str1_name, *str1_age; printf("str1:%s\n\n",str1); str1_json = cJSON_Parse(str1); // create a JSON parsing object that returns the correct JSON format if (! Str1_json) {printf("JSON format error :%s\n\n", cJSON_GetErrorPtr()); } else {printf(" json format correct :\n%s\n\n",cJSON_Print(str1_json)); str1_name = cJSON_GetObjectItem(str1_json, "name"); If (str1_name->type == cJSON_String) {printf(" name :%s\r\n", str1_name-> valueString); } str1_age = cJSON_GetObjectItem(str1_json, "age"); If (str1_age->type==cJSON_Number) {printf(" age :%d\r\n", str1_age->valueint); } cJSON_Delete(str1_json); // Free memory}}Copy the code

Running results:

JSON string parsing containing arrays

JSON string:

{" location ": [{" name" : "Faye", "address" : "Beijing"}, {" name ":" Andy ", "address" : "Hong Kong"}], "time" : "2018-11-17"}Copy the code

[External link image transfer failed, source station may have anti-theft mechanism, it is recommended to save the image directly upload (IMG-N6vxqg6T-1569933943633)(www.json.org/array.gif)]

Analytic function:

void Parse_Str2(void) { char str2[] = "{\" location \ ": [{\" name \ ": \" Faye \ ", \ "address \" : \ \ "Beijing"}, {\ "name \" : \ "Andy \", \ "address \" : \ \ "Hong Kong"}], \ "time \" : \ "2018-11-17 \"}" ; cJSON *root = 0; cJSON *loc_json = 0; cJSON *name1_json,*name2_json; char *time_str, *str_tmp; root = cJSON_Parse(str2); if(! Root) printf("str2 JSON format error :%s \r\n", cJSON_GetErrorPtr()); Else {printf("str2 JSON format correct :\n%s\n",cJSON_Print(root)); time_str = cJSON_GetObjectItem(root,"time")->valuestring; Printf ("time:%s\n", time_str); loc_json = cJSON_GetObjectItem(root,"location"); if(loc_json) { name1_json = cJSON_GetArrayItem(loc_json,0); Str_tmp = cJSON_GetObjectItem(name1_json, "name")-> valueString; Printf ("name1 is: %s \r\n", str_tmp); printf("name1 is: %s \r\n", str_tmp); str_tmp = cJSON_GetObjectItem(name1_json, "address")->valuestring; Printf ("addr1 is: %s \r\n", str_tmp); name2_json = cJSON_GetArrayItem(loc_json,1); Str_tmp = cJSON_GetObjectItem(name2_json, "name")-> valueString; printf("name2 is : %s \r\n", str_tmp); str_tmp = cJSON_GetObjectItem(name2_json, "address")->valuestring; printf("addr2 is : %s \r\n", str_tmp); } } cJSON_Delete(loc_json); }Copy the code

Running results:

Beijing time JSON data parsing

API address:

http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json

JSON string:

{ "success": "1", "result": { "timestamp": "1543922613", "datetime_1": "2018-12-04 19:23:33", "datetime_2": "On December 4, 2018, 19 23 minutes 33 seconds", "week_1" : "2", "week_2" : "Tuesday", "week_3" : "Tuesday", "week_4" : "Tuesday"}}Copy the code

Analytic function:

void Parse_BJ_Time(void) { char bj_time_str[] = "{\"success\":\"1\",\"result\":{\"timestamp\":\"1542456793\",\"datetime_1\":\"2018-11-17 20:13:13\",\"datetime_2\":\" November 17, 2018 13 when 20 minutes and 13 seconds \ ", \ "week_1 \" : \ "6 \", \ "week_2 \ \", \ "Saturday", \ "week_3 \ \", \ "Saturday", \ "week_4 \" : \ "Saturday \"}} "; cJSON *root; cJSON *result_json; char *datetime, *week; root = cJSON_Parse(bj_time_str); If (root) {printf("json format correct :\n%s\n\n", cJSON_Print(root)); result_json = cJSON_GetObjectItem(root, "result"); If (result_json) {datetime = cJSON_GetObjectItem(result_json, "datetime_2")-> valueString; Printf (" Beijing time: %s \r\n", datetime); week = cJSON_GetObjectItem(result_json, "week_2")->valuestring; Printf (" week: %s \r\n", week); } } cJSON_Delete(root); cJSON_Delete(result_json); }Copy the code

Running results:

Heart knows weather JSON data parsing

JSON string:

{" results ": [{" location" : {" id ":" WS10730EM8EV ", "name" : "shenzhen", "country" : "CN", "path" : "shenzhen, shenzhen, guangdong, China", "timezone" : "Asia/Shanghai", "timezone_offset" : "+ 08:00}", "daily" : [{" date ":" 2018-11-18 ", "text_day" : "cloudy", "code_day" : "4", "text_night" : "cloudy", "code_night" : "4", "high" : "26", "low" : "20", "precip" : ""," wind_direction ": "No sustained wind", "wind_direction_degree" : ""," wind_speed ":" 10 ", "wind_scale" : "2"}, {" date ":" 2018-11-19 ", "text_day" : "Rain", "code_day" : "13", "text_night" : "rain", "code_night" : "13", "high" : "25", "low" : "20", "precip" : ""," wind_direction ":" no sustained wind ", "wind_direction_degree" : ""," wind_speed ":" 10 ", "wind_scale" : "2"}, {" date ": "2018-11-20", "text_day" : "rain", "code_day" : "13", "text_night" : "rain", "code_night" : "13", "high" : "25", "low" : "21", "precip" : ""," wind_direction ":" no sustained wind ", "wind_direction_degree" : ""," wind_speed ":" 10 ", "wind_scale" : "2" }], "last_update": "2018-11-18T11:00:00+08:00" }] }Copy the code

Analytic function:

void parse_seniverse_weather(void) { char weather_str[] = "{\" results \ ": [{\" location \ ": {\" id \ ": \" WS10730EM8EV \ ", \ "name \" : \ \ "" shenzhen, \" country \ ": \" CN \ ", \ "path \" : \ "shenzhen, shenzhen, guangdong, China \" and \ "time Zone \ ", \ "Asia/Shanghai \" and \ "+ 08:00 timezone_offset \", \ "\"}, \ "daily \" : [{\ "date \" : \ "2018-11-18 \", \ "text_day \" : \ "cloudy \", \ "code_ Day \ ": \" 4 \ ", \ "cloudy text_night \", \ "\", \ "code_night \", \ "4 \" and \ "on \ \", \ "26", \ "low \", \ "20 \" and \ "precip \", \ "\", \ "wind_direction \" : \ "no sustained wind \", \ "wind_direction_degree \" : \ "\", \ "wind_speed \" : \ "10 \", \ "wind_scale \" : \ \ "2"}, {\ "date \" : \ "2018-11-19 \", \ "text_da Y \ ": \" light rain \ ", \ "13 code_day \", \ "\", \ "text_night \", \ "light rain \" and \ "13 code_night \", \ "\", \ "high \", \ "25 \", \ "low \", \ "20 \", \ "precip \" : \ \" ", \ "wind_direction \", \ "no sustained wind \" and \ "wind_direction_degree \", \ "\", \ "10 wind_speed \", \ "\", \ "wind_scale \ \", \ "2"}, {\ "date \" : \ "20 The 18-11-20 \ ", \ "text_day \", \ "light rain \" and \ "13 code_day \", \ "\", \ "text_night \", \ "light rain \" and \ "code_night \", \ "13 \" and \ "on \ \", \ "25", \ "low \" : \ "21 \", \ "precip \" : \ "\", \ "wind_direction \" : \ "no sustained wind \", \ "wind_direction_degree \" : \ "\", \ "wind_speed \" : \ \ "10", \ "wind_scale \" : \ "2\"}],\"last_update\":\"2018-11-18T11:00:00+08:00\"}]}"; cJSON *root; cJSON *results; cJSON *last_update; cJSON *loc_json, *daily_json; cJSON *forecast_json; char *loc_tmp, *weather_tmp, *update_tmp; int i = 0; root = cJSON_Parse((const char*)weather_str); If (root) {/ / printf (" JSON format right: \ n \ n \ n % s ", cJSON_Print (root)); // Output json string results = cJSON_GetObjectItem(root, "results"); results = cJSON_GetArrayItem(results,0); if(results) { loc_json = cJSON_GetObjectItem(results, "location"); Loc_tmp = cJSON_GetObjectItem(loc_json, "id") -> valueString; Printf (" city ID: % s \ n ", loc_tmp); loc_tmp = cJSON_GetObjectItem(loc_json, "name") -> valuestring; Printf (" City name :%s\n",loc_tmp); loc_tmp = cJSON_GetObjectItem(loc_json, "timezone") -> valuestring; Printf (" city time zone :%s\n\n",loc_tmp); daily_json = cJSON_GetObjectItem(results, "daily"); 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 (" date :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "code_day") -> valuestring; Printf (" daytime weather code :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "code_night") -> valuestring; Printf (" Evening weather code :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "high") -> valuestring; Printf (" Max temperature :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "low") -> valuestring; Printf (" minimum temperature :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_direction_degree") -> valuestring; Printf (" Wind Angle :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_scale") -> valuestring; Printf (" wind :%s\r\n\n", weather_tmp); }} else printf("daily JSON format error \r\n"); last_update = cJSON_GetObjectItem(results, "last_update"); update_tmp = last_update->valuestring; If (last_update) {printf(" update time :%s\r\n", update_tmp); }} else {printf(" Results format error :%s\r\n", cJSON_GetErrorPtr()); }} else {printf("JSON format error \r\n"); } cJSON_Delete(root); cJSON_Delete(results); }Copy the code

Running results:

Analysis of wind weather data

JSON string:

{" HeWeather6 ": [{" basic" : {" cid ":" CN101010700 ", "location" : "changping", "parent_city" : "Beijing", "admin_area" : "Beijing", "cnty" : "Chinese", "lat" : "40.21808624", "says lon" : "116.23590851", "tz" : "+ 8.00"}, "update" : {" loc ":" the 2018-11-21 when ", "utc" : "2018-11-21 13:45" }, "status": "ok", "daily_forecast": [{ "cond_code_d": "100", "cond_code_n": "100", "cond_txt_d": "Fine," "cond_txt_n" : "clear", "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" : "northwest wind", "wind_sc" : "1-2", "wind_spd" : "4"}, {" cond_code_d ":" 100 ", "cond_code_n" : "101", "cond_txt_d" : "clear", "cond_txt_n" : "cloudy", "date" : "2018-11-22", "hum" : "21", "Mr" : "blessed", "ms" : "05:33", "PCPN" : "0.0" and "pop", "0" and "pres" : "1030", "sr" : "07:09," "ss" : "hast judged", "tmp_max" : "8", "tmp_min" : "4", "uv_index" : "3", "vis" : "20", "wind_deg" : "35", "wind_dir" : "northeaster," "wind_sc" : "1-2", "wind_spd" : "5"}, {" cond_code_d ": "101", "cond_code_n" : "100", "cond_txt_d" : "cloudy", "cond_txt_n" : "clear", "date" : "2018-11-23", "hum" : "23", "Mr" : "He", "ms" : "06:41", "PCPN" : "0.0", "pop" : "16" and "pres" : "1024", "sr" : "07:10", "ss" : "hast judged", "tmp_max" : "7", "tmp_min" : "2", "uv_index" : "2", "vis" : "20", "wind_deg" : "305", "wind_dir" : "northwest wind", "wind_sc" : "1-2", "wind_spd" : "3"}}}]]Copy the code

Analytic function:

// Parse the wind weather, Void parse_heweather(void) {char heweather_str[] = "{\" HeWeather6 \ ": [{\ \" ", basic: {\ "cid \" : \ "CN101010700 \", \ "the location \" : \ "changping \", \ "parent_city \" : \ \ "Beijing", \ "admin_area \" : \ \ ", "Beijing, \ "Cnty \" : \ "China \", \ "lat \" : \ "40.21808624 \", \ "says lon \" : \ "116.23590851 \", \ "tz \" : \ \ "+ 8.00"}, \ "update \" : {\ "loc \" : \ "in the 2018-11-21 s 21:45\",\"utc\":\"2018-11-21 When \}, \ "status \", \ "ok \" and \ "daily_forecast \" : [{\ "cond_code_d \" : \ "100 \" and \ "cond_code_n \" : \ "100 \", \ "cond_txt_d \" : \ "or \", \ "c Ond_txt_n \ ", \ "or \", \ "the date \" : \ "2018-11-21 \" and \ "hum \", \ "21 \" and \ "Mr \" : \ "16:02 \", \ "\", \ "ms 04:27 \" and \ "PCPN \ \", \ "0.0", \ "pop \ \", \ "0 ",\"pres\":\"1030\",\"sr\":\"07:08\",\"ss\":\"16:53\",\"tmp_max\":\"9\",\"tmp_min\":\"-3\",\"uv_index\":\"5\",\"vis\":\" Wind_deg 10 \ ", \ "\", \ "323 \" and \ "wind_dir \" : \ "mistral \" and \ "wind_sc \", \ "1-2 \" and \ "wind_spd \" : \ "4 \"}, {\ "cond_code_d \" : \ "100 \" and \ "cond_c Ode_n \ ": \" 101 \ "and \" cond_txt_d \ ": \" clear \ "and \" cloudy cond_txt_n \ ", \ "\", \ "the date \" : \ "2018-11-22 \" and \ "hum \", \ "21 \" and \ "Mr \" : \ "blessed \" and \ "ms \ 05:33 ": \" \ ", \ "PCPN \ \", \ "0.0", \ "pop \" : \ "0 \", \ "pres \" : \ "1030 \" and \ "sr \" : \ "07:09 \" and \ "ss \" : \ "hast judged \" and \ "eight tmp_max \", \ "\", \ "tmp_m In \ ": \" 4 \ ", \ "uv_index \", \ "3 \", \ "vis \", \ "20 \" and \ "35 wind_deg \", \ "\", \ "wind_dir \" : \ "northeaster \" and \ "wind_sc \", \ "1-2 \" and \ "wind_spd \" : 5 \ \ ""}, {\" cond_code_d \ ": \" 101 \ "and \" cond_code_n \ ": \" 100 \ ", \ "cond_txt_d \" : \ "cloudy \", \ "cond_txt_n \" : \ "or \", \ "the date \" : \ "in the 2018-11-23 s \ ", \ "hum \" : \ "and \", \ "Mr \", \ "he \", \ "\", \ "ms 06:41 \" and \ "PCPN \ \", \ "0.0", \ "pop \", \ "16 \", \ "pres \" : \ "1024 \", \ "the sr \" : \ "07:10 \", \ "ss\":\"16:52\",\"tmp_max\":\"7\",\"tmp_min\":\"-2\",\"uv_index\":\"2\",\"vis\":\"20\",\"wind_deg\":\"305\",\"wind_dir\" : \ "mistral \", \ "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"); Results = cJSON_GetArrayItem(results,0); 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 (" city ID: % s \ n ", basic_tmp); basic_tmp = cJSON_GetObjectItem(basic_json, "location") -> valuestring; Printf (" county-level city :%s\n",basic_tmp); basic_tmp = cJSON_GetObjectItem(basic_json, "parent_city") -> valuestring; Printf (" prefecture-level city :%s\n",basic_tmp); basic_tmp = cJSON_GetObjectItem(basic_json, "admin_area") -> valuestring; Printf (" Province :%s\n",basic_tmp); basic_tmp = cJSON_GetObjectItem(basic_json, "lat") -> valuestring; Printf (" latitude: % s \ n ", basic_tmp); basic_tmp = cJSON_GetObjectItem(basic_json, "lon") -> valuestring; Longitude: printf (" % s \ n \ n ", basic_tmp); } update_json = cJSON_GetObjectItem(results, "update"); if(update_json) { update_tmp = cJSON_GetObjectItem(update_json, "loc") -> valuestring; Printf (" update time :%s(local time)\n", update_tmp); update_tmp = cJSON_GetObjectItem(update_json, "utc") -> valuestring; Printf (" update time :%s(world time)\n\n", update_tmp); } status_tmp = cJSON_GetObjectItem(results, "status") -> valuestring; Printf (" parse status :%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 (" date :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "cond_txt_d") -> valuestring; Printf (" Daytime weather :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "cond_txt_n") -> valuestring; Printf (" Evening weather :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "tmp_max") -> valuestring; Printf (" Max temperature :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "tmp_min") -> valuestring; Printf (" minimum temperature :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_deg") -> valuestring; Printf (" Wind Angle :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_dir") -> valuestring; Printf (" Wind direction :%s\r\n", weather_tmp); weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_sc") -> valuestring; Printf (" wind :%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); }Copy the code

Running results:

Source code download and practical API address:

  • This project CodeBlock project source code download: MyJSON
  • Online JSON format verification tool: BEJSON
  • Free weather API: Weather API

Selected historical articles:

Welcome to my personal blog

Scan the code on wechat to follow my public account

Update personal study notes and technical summary of electronic embedded from time to time, welcome to learn and communicate with each other!