色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

c 解析12306json

12306是中國鐵路客運(yùn)網(wǎng)站,提供了火車票購買、退改簽等服務(wù)。為了提供更好的用戶體驗(yàn),網(wǎng)站在前端與后端之間采用了基于JSON格式的數(shù)據(jù)交互方式。C語言是一種強(qiáng)大的編程語言,可以用來解析JSON數(shù)據(jù)。

要解析JSON數(shù)據(jù),需要使用第三方庫。其中較為流行的有cJSONjson-c等。這里以cJSON為例,介紹如何在C語言中使用cJSON解析12306的JSON數(shù)據(jù)。

#include <stdio.h>#include <stdlib.h>#include <cJSON.h>int main()
{
char json_str[] = "{\"status\":true,\"data\":[{\"train_no\":\"240000G1010F\",\"station_train_code\":\"G101\",\"start_station_telecode\":\"BJP\",\"end_station_telecode\":\"SHH\",\"from_station_telecode\":\"BJP\",\"to_station_telecode\":\"SHH\",\"start_time\":\"06:43\",\"arrive_time\":\"12:58\",\"day_difference\":\"0\",\"train_seat_feature\":\"O9M130\",\"lishi\":\"06:15\",\"swz_num\":\"--\",\"zy_num\":\"10\",\"ze_num\":\"--\",\"gr_num\":\"--\",\"rw_num\":\"--\",\"yw_num\":\"--\",\"rz_num\":\"--\",\"yz_num\":\"--\",\"wz_num\":\"無\"}]}";
cJSON *json, *status, *data, *train_data, *train_no, *station_train_code, *start_time, *zy_num; 
json = cJSON_Parse(json_str);
if (json == NULL) {
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
return 1;
}
status = cJSON_GetObjectItem(json, "status");
if (status->valueint == 0) {
printf("There is an error\n");
return 1;
}
data = cJSON_GetObjectItem(json, "data");
if (cJSON_GetArraySize(data) == 0) {
printf("There is no data\n");
return 1;
}
train_data = cJSON_GetArrayItem(data, 0);
train_no = cJSON_GetObjectItem(train_data, "train_no");
station_train_code = cJSON_GetObjectItem(train_data, "station_train_code");
start_time = cJSON_GetObjectItem(train_data, "start_time");
zy_num = cJSON_GetObjectItem(train_data, "zy_num");
printf("train_no: %s \n", train_no->valuestring);
printf("station_train_code: %s \n", station_train_code->valuestring);
printf("start_time: %s \n", start_time->valuestring);
printf("zy_num: %s \n", zy_num->valuestring);
cJSON_Delete(json);
return 0;
}

以上代碼演示了如何解析從12306返回的車次信息。json_str為從12306返回的JSON字符串,通過cJSON_Parse函數(shù)把它解析成cJSON類型。然后,通過cJSON_GetObjectItem和cJSON_GetArrayItem等函數(shù),可以方便地獲取JSON數(shù)據(jù)的各個字段。在獲取到數(shù)據(jù)后,通過cJSON_Delete函數(shù)釋放內(nèi)存。