Skip to content

Instantly share code, notes, and snippets.

@SimpleDrunk
Last active December 27, 2015 15:59
Show Gist options
  • Save SimpleDrunk/7351790 to your computer and use it in GitHub Desktop.
Save SimpleDrunk/7351790 to your computer and use it in GitHub Desktop.
[code page] ofxJson天气预报解析,有issue,有时得到的数据不正确。
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
font.loadFont("fzzhong.ttf", 96, true, true);
font_LT23.loadFont("LT23.ttf", 30, true, true);
font_ado.loadFont("Ado.otf", 24, true, true);
bgImg.loadImage("img/bg.png");
//设置区域
std::wcout.imbue(std::locale("CHS"));
timeMark = -601.0f;
bDateCheck = false;
dateNum = 0;
}
//--------------------------------------------------------------
void testApp::update(){
if(ofGetElapsedTimef() - timeMark > 600.0f){ //每10分钟更新一次数据
string url = "http://www.weather.com.cn/data/sk/101200101.html"; //武汉天气的接口地址
bool parsingSuccessful = nowJson.open(url);
if(parsingSuccessful) {
city = nowJson["weatherinfo"]["city"].asString();
temp = nowJson["weatherinfo"]["temp"].asString();
WD = nowJson["weatherinfo"]["WD"].asString();
WS = nowJson["weatherinfo"]["WS"].asString();
SD = nowJson["weatherinfo"]["SD"].asString();
time = nowJson["weatherinfo"]["time"].asString();
if(nowJson.isConvertibleTo(Json::arrayValue))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
string all = nowJson.getRawString();
wfstream wf;
wf.open("a.txt", ios::app|ios::out);
wf<<all.c_str()<<"\n";
wf.close();
timeMark = ofGetElapsedTimef();
cout<<"MARK 1\n";
}
GetLocalTime(&stTime);
if(!bDateCheck || stTime.wDay != dateNum) { //得到每日详细天气信息
string url_ = "http://m.weather.com.cn/data/101200101.html";
bool parsingSuccessful = dateJson.open(url_);
if(parsingSuccessful) {
date = dateJson["weatherinfo"]["date"].asString();
date_y = dateJson["weatherinfo"]["date_y"].asString();
week = dateJson["weatherinfo"]["week"].asString();
index = dateJson["weatherinfo"]["index"].asString();
index_d = dateJson["weatherinfo"]["index_d"].asString();
index_uv = dateJson["weatherinfo"]["index_uv"].asString();
index_xc = dateJson["weatherinfo"]["index_xc"].asString();
index_tr = dateJson["weatherinfo"]["index_tr"].asString();
index_cl = dateJson["weatherinfo"]["index_cl"].asString();
index_ls = dateJson["weatherinfo"]["index_ls"].asString();
index_ag = dateJson["weatherinfo"]["index_ag"].asString();
for(int i=1; i<=6; i++){
string a, b, c;
a = "temp" + ofToString(i);
b = "weather" + ofToString(i);
c = "wind" + ofToString(i);
temps[i-1] = dateJson["weatherinfo"][a].asString();
weatheres[i-1] = dateJson["weatherinfo"][b].asString();
winds[i-1] = dateJson["weatherinfo"][c].asString();
}
cout<<"MARK 2\n";
}
bDateCheck = true;
dateNum = stTime.wDay;
cout<<"MARK 3\n";
}
}
//--------------------------------------------------------------
void testApp::draw(){
bgImg.draw(0, 0, 480, 800);
//显示实时时间
// font_LT23.drawString(ofToString(stTime.wHour)+":"+ofToString(stTime.wMinute), 50, 100 );
//显示日期
// font_ado.drawString(stringToWstring(week), 50, 150);
// wcout<<stringToWstring(date_y)<<"\n";
//显示实时温度
// font.drawString(temp, 50, 300);
// font.drawString(L"℃", 200, 300);
ofDrawBitmapString(nowJson.getRawString(), 50, 14);
}
//--------------------------------------------------------------
wchar_t* testApp::stringToWstring(string src) {
int len = src.size();
int wcslen = ::MultiByteToWideChar(CP_UTF8, NULL, src.c_str(), len, NULL, 0);
wchar_t* wszString = new wchar_t[wcslen + 1];
::MultiByteToWideChar(CP_UTF8, NULL, city.c_str(), len, wszString, wcslen);
wszString[wcslen] = L'\0';
return wszString;
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
@SimpleDrunk
Copy link
Author

//head file

pragma once

include "ofMain.h"

include "ofxTrueTypeFontUc.h"

include "ofxJSONElement.h"

class testApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

void keyPressed  (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
wchar_t* stringToWstring(string src);

ofxJSONElement nowJson, dateJson;
ofxTrueTypeFontUC font, font_LT23, font_ado;
float timeMark;
ofImage bgImg;
bool bDateCheck;
int dateNum;
SYSTEMTIME stTime;

string city, temp, WD, WS, SD, time;        //实时信息
string temps[6], weatheres[6], winds[6];    //近6天的温度,天气,风力
string date, date_y, week, index, index_d, index_uv, index_xc, index_tr, index_cl, index_ls, index_ag;

};

@SimpleDrunk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment