C++查询本地天气 VS2005

之前看到一篇文章写得很有意思,自己用VS2005尝试着做,发现很多问题尴尬


1、中央气象台API返回的JSON数据(http://m.weather.com.cn/data/101010100.html

2、外网获取IP(http://ip.dnsexit.com/index.php

//3、Sqlite(1中的城市代号利用sqlite3实现查询)

4、C++中GB2312字符串和UTF-8之间的转换(见这篇文章http://blog.csdn.net/lgh1992314/article/details/8579206

5、Jsoncpp(主要是处理1中的数据==见这篇文章http://blog.csdn.net/lgh1992314/article/details/8582179

以上转至http://blog.csdn.net/x_iya/article/details/16834343


http://sourceforge.net/projects/jsoncpp上下载JSon源码,编译得到静态库。

进入jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71 打开jsoncpp.sln,右键lib_Json-生成静态库,文件在jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\debug\lib_json文件夹中;

注:右键lib_Json-属性-配置属性-C/C++-代码生成-运行时库-“多线程调试(/MTD)”


接下来建立一个项目来查询天气,暂时只查询本地(深圳天气)未用到Sqlite;

VS2005配置如下:

属性-配置属性-常规-MFC的使用-在静态库中使用MFC
属性-配置属性-常规-字符集-使用多字节字符集
属性-配置属性-C/C++-代码生成-运行时库-多线程调试(/MTD)
属性-配置属性-连接器-输入-附加依赖项-json_vc71_libmtd.lib nafxcwd.lib libcmtd.lib


将json_vc71_libmtd.lib库和jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\下的json文件夹导入当前工程目录,

在头文件中写入#include "json/json.h"

// testJson.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "json/json.h"
#include <string>
#include <iostream>
#include <afxinet.h>

std::string GetWeb(const char* _pUrl)
{
CInternetSession session("Httpclient");


CHttpFile* pFile = (CHttpFile*)session.OpenURL(_pUrl);


CString csData;
CString csContent;
DWORD dwStatusCode;
pFile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode == 200)
{
while (pFile->ReadString(csData))
{
csContent += csData;
}
}
return std::string(csContent);
}


char* UTF8ToGB2312(const char* _p8Data)
{
int nLen = MultiByteToWideChar(CP_UTF8, 0, _p8Data, -1, NULL, 0);//获取UTF-8编码下多字节数据的长度
wchar_t* pw8Dada = new wchar_t[nLen+1];//+1 给‘\0’
MultiByteToWideChar(CP_UTF8, 0, _p8Data, -1, pw8Dada, nLen);//将UTF-8编码下多字节数据转换成UTF-8编码的宽字节数据


nLen = WideCharToMultiByte(CP_ACP, 0, pw8Dada, -1, NULL, 0, NULL, NULL);//获取ANSI编码下宽字节数据的长度
char* pGB2312Data = new char[nLen+1];//此处还有一个释放问题未解决
WideCharToMultiByte(CP_ACP, 0, pw8Dada, -1, pGB2312Data, nLen, NULL, NULL);//将ANSI编码下宽字节数据转换成ANSI编码的多字节数据


if (pw8Dada)
{
delete []pw8Dada;
}
return pGB2312Data;
}


void JsonWeather()
{
std::string str8Dada = GetWeb("http://m.weather.com.cn/data/101280601.html");
    std::string strGB2312Dada(UTF8ToGB2312(str8Dada.c_str()));
Json::Value Weather;
Json::Reader reader;
reader.parse(strGB2312Dada, Weather);


std::cout<<Weather["weatherinfo"]["city"]<<std::endl;
    std::cout<<Weather["weatherinfo"]["date_y"]<<std::endl;
std::cout<<Weather["weatherinfo"]["week"]<<std::endl;
std::cout<<Weather["weatherinfo"]["cityid"]<<std::endl;
std::cout<<Weather["weatherinfo"]["temp1"]<<std::endl;




}


int _tmain(int argc, _TCHAR* argv[])
{

JsonWeather();
getchar();
return 0;
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值