心知天气数据JSON格式介绍
JSON格式介绍http://t.csdnimg.cn/pJX1n
下面代码是利用CJSON库进行数据解析
解析代码
#include <stdio.h>
#include <string.h>
#include "cJSON.h" // 假设你的CJSON库头文件路径是正确的
int main(void) 
{
    // 提供的JSON数据
    const char *json_data = "{\"results\":[{\"location\":{\"id\":\"WS10730EM8EV\",\"name\":\"深圳\",\"country\":\"CN\",\"path\":\"深圳,深圳,广东,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"now\":{\"text\":\"阴\",\"code\":\"9\",\"temperature\":\"27\"},\"last_update\":\"2024-06-11T22:00:15+08:00\"}]}";
    // 解析JSON数据
    cJSON *root = cJSON_Parse(json_data);
    if (root == NULL) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\njson_data是非法的JSON数据\r\n");
        printf("\r\n===================================================\r\n");
        return 1;
    }
    else
    {
    	printf("\r\n===================================================\r\n");
    	printf("\r\njson_data是合法的JSON数据\r\n");
    	printf("\r\n===================================================\r\n");
	}
    // 检查JSON数据类型是否为对象
    if (!cJSON_IsObject(root)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nJSON数据 is not an 对象\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
    else
    {
    	printf("\r\n===================================================\r\n");
    	printf("\r\nJSON数据 is  an 对象\r\n");
    	printf("\r\n===================================================\r\n");
	}
    // 获取 results 数组
    cJSON *results = cJSON_GetObjectItemCaseSensitive(root, "results");
    if (!cJSON_IsArray(results)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nResults数据 is not an 数组\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
    else
	{
		printf("\r\n===================================================\r\n");
		printf("\r\nResults数据 is an 数组\r\n");
		printf("\r\n===================================================\r\n");
	} 
    // 解析 results 数组中的第一个对象
    cJSON *first_result = cJSON_GetArrayItem(results, 0);
    if (!cJSON_IsObject(first_result)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nFirst result数据 is not an 对象\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
	else
	{
		printf("\r\n===================================================\r\n");
		printf("\r\nFirst result数据 is an 对象\r\n");
		printf("\r\n===================================================\r\n");
	}
    // 获取 location 对象
    cJSON *location = cJSON_GetObjectItemCaseSensitive(first_result, "location");
    if (!cJSON_IsObject(location)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nLocation 数据 is not an 对象\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
    else
    {
    	printf("\r\n===================================================\r\n");
    	printf("\r\nLocation 数据 is  an 对象\r\n");
    	printf("\r\n===================================================\r\n");
	}
    // 获取 now 对象
    cJSON *now = cJSON_GetObjectItemCaseSensitive(first_result, "now");
    if (!cJSON_IsObject(now)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nNow 数据 is not an 对象\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
    else
    {
    	printf("\r\n===================================================\r\n");
    	printf("\r\nNow 数据 is an 对象\r\n");
    	printf("\r\n===================================================\r\n");
	}
    // 获取 last_update 字符串
    cJSON *last_update = cJSON_GetObjectItemCaseSensitive(first_result, "last_update");
    if (!cJSON_IsString(last_update)) 
	{
		printf("\r\n===================================================\r\n");
        printf("\r\nLast update time is not a 字符串\r\n");
        printf("\r\n===================================================\r\n");
        cJSON_Delete(root);
        return 1;
    }
	else
	{
		printf("\r\n===================================================\r\n");
		printf("\r\nLast update time is a 字符串\r\n");
		printf("\r\n===================================================\r\n");
	}
	
    // 提取各个字段的数据
    const char *location_id              = cJSON_GetObjectItemCaseSensitive(location, "id")->valuestring;
    const char *location_name            = cJSON_GetObjectItemCaseSensitive(location, "name")->valuestring;
    const char *location_country         = cJSON_GetObjectItemCaseSensitive(location, "country")->valuestring;
    const char *location_path            = cJSON_GetObjectItemCaseSensitive(location, "path")->valuestring;
    const char *location_timezone        = cJSON_GetObjectItemCaseSensitive(location, "timezone")->valuestring;
    const char *location_timezone_offset = cJSON_GetObjectItemCaseSensitive(location, "timezone_offset")->valuestring;
    const char *now_text                 = cJSON_GetObjectItemCaseSensitive(now, "text")->valuestring;
    const char *now_code                 = cJSON_GetObjectItemCaseSensitive(now, "code")->valuestring;
    const char *now_temperature          = cJSON_GetObjectItemCaseSensitive(now, "temperature")->valuestring;
    const char *last_update_time         = cJSON_GetStringValue(last_update);
    printf("===================================================\r\n");
    // 打印解析结果
    printf("地点ID:   %s\r\n", location_id);
    printf("地点名字: %s\r\n", location_name);
    printf("所在国家: %s\r\n", location_country);
    printf("地点信息: %s\r\n", location_path);
    printf("时区信息: %s\r\n", location_timezone);
    printf("时区偏移: %s\r\n", location_timezone_offset);
    printf("天气状况: %s\r\n", now_text);
    printf("天气代码: %s\r\n", now_code);
    printf("温度信息: %s 摄氏度\r\n", now_temperature);
    printf("更新时间: %s\r\n", last_update_time);
    printf("===================================================\r\n");
    printf("===================================================\r\n");
    // 释放资源
    cJSON_Delete(root);
    return 0;
}
 
解析结果


![[工具探索]英寸vs毫米下常见尺寸排版](https://img-blog.csdnimg.cn/direct/ceea9c79d5c34964a96d2b59e381b237.jpeg#pic_center)














