C++实现查询天气预报

原地址:http://blog.csdn.net/x_iya/article/details/8583015

一、新的变化

1、使用的开发环境是VS 2019
2、原程序中使用的很多API接口已经失效,对API进行了更新
3、对程序输出进行了简单改写,可以循环查询城市天气

二、用到的API接口

1、本机IP查询:
http://ip.dnsexit.com/
2、IP相关信息查询:
http://ip-api.com/json/?lang=zh-CN&ip=IP地址
3、天气相关信息查询:
http://api.help.bj.cn/apis/weather/?id=城市代码

三、用到的函数

1、sqlite
通过城市名称利用sqlite3查询城市代号
2、Jsoncpp
对IP信息和天气信息进行处理
3、C++中GB2312字符串和UTF-8之间的转换:http://blog.csdn.net/lgh1992314/article/details/8579206

四、sqlite教程

1、下载地址:
https://www.sqlite.org/download.html
2、sqlite菜鸟教程:
https://www.runoob.com/sqlite/sqlite-tutorial.html
3、sqlite配置下载安装教程:https://www.cnblogs.com/weibanggang/p/9478492.html
4、如何在Windows 10上构建SQLite3.lib文件:https://blog.csdn.net/m0_37909265/article/details/105102982
5、sqlite可视化工具:
http://sqliteadmin.orbmu2k.de/

五、Jsoncpp教程

1、下载地址:
https://sourceforge.net/projects/jsoncpp/
2、windows下JSonCpp的编译和使用:
https://www.cnblogs.com/hpcpp/p/7889456.html
3、使用 C++ 处理 JSON 数据交换格式:http://blog.csdn.net/lgh1992314/article/details/8582179
4、JsonCpp中文unicode编码问题:
http://blog.csdn.net/x_iya/article/details/16833663

六、代码

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <locale>  
#include <codecvt>  
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <afxinet.h>
#include <windows.h>

#include "include/sqlite/sqlite3.h"
#include "include/json/json.h"

#pragma comment(lib,"sqlite3.lib")
#pragma comment(lib,"json_vc71_libmtd.lib")
using namespace Json;
using namespace std;
std::string City_code;
std::string country;
std::string city;
std::string province;
std::string isp;

int sqlite3_exec_callback(void* data, int n_columns, char** col_values, char** col_names)  //sqlite数据库的回调函数
{
   
	//int i;
	/*for(i=0; i < n_columns; i++)
	{
		cout << col_values[i] << "\t";
	}
	cout << endl;*/
	City_code = col_values[0];  //返回从数据库查询到的城市代码
	return 0;
}


void ConvertUTF8ToANSI()  //对天气的json数据运用c++11来进行UTF8和ANSI的类型转化
{
   
	auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
	std::wifstream wfin("temp.json");
	std::wstring wstr, content;
	wfin.imbue(LocUtf8);
	while (getline(wfin, wstr))
	{
   
		content += wstr;
	}
	wfin.close();
	system("del temp.json");
	//std::wcout.imbue(std::locale(""));
	//std::wcout << content << std::endl;  

	std::locale::global(std::locale("Chinese-simplified"));
	std::wofstream wfout("weather.json");
	wfout << content;
	wfout.close();
}


void ConvertIPUTF8ToANSI()  //对IP的json数据运用c++11来进行UTF8和ANSI的类型转化
{
   
	auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
	std
  • 6
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值