Exemple #1
0
func main() {
	// urlにアクセス
	resp, err := http.Get(tenki_url)

	if err != nil {
		panic(err)
	}

	if resp.StatusCode != http.StatusOK {
		panic(resp.StatusCode)
	}

	// jsonパース
	json, err := easyjson.NewEasyJson(resp.Body)
	if err != nil {
		panic("json convert err")
	}

	fmt.Println(json.K("title"))

	for _, content := range json.K("forecasts").RangeObjects() {
		dateLabel, _ := content.K("dateLabel").AsString()
		if dateLabel == "今日" {
			today, _ := content.K("date").AsString()
			weather, _ := content.K("telop").AsString()
			description, _ := json.K("description").K("text").AsString()

			tenki_text += today + "の東京の天気は " + weather + " です\n\n" + description
		}
	}

	fmt.Println(tenki_text)

	// 条件次第でpepper_botに投げる
}
Exemple #2
0
func channelHandleFunc(
	fromClient chan interface{},
	toClient chan interface{}) func(w http.ResponseWriter, req *http.Request) {

	echoHandler := func(ws *websocket.Conn) {
		//		ws.JSON.re
		for {
			select {
			case value := <-fromClient:
				//				websocket.JSON.Send(ws,value)
				//				ws.Read(msg)
				data, err := easyjson.NewEasyJson(value)
				if err != nil {
					panic("error")
				}
				fmt.Fprintf(ws, "%s", data)

			default:
				//				JSON.Receive(ws,&data)
				// msg := make([]byte, 512)
				readdata, err := easyjson.NewEasyJson(ws)
				if err != nil {
					panic("error")
				}

				toClient <- readdata

			}
		}
	}

	return func(w http.ResponseWriter, req *http.Request) {
		s := websocket.Server{Handler: websocket.Handler(echoHandler)}
		s.ServeHTTP(w, req)

	}
}
Exemple #3
0
func run() error {
	resp, err := http.Get("https://teratail.com/api/v1/questions")
	if err != nil {
		return fmt.Errorf("Failed to connect teratail.com")
	}
	defer resp.Body.Close()

	jsonData, err := easyjson.NewEasyJson(resp.Body)
	if err != nil {
		return fmt.Errorf("Invalid responses")
	}

	fmt.Println(jsonData.K("questions", 0))
	for k, v := range jsonData.K("questions").RangeObjects() {
		fmt.Printf("key: %d, value: %s\n", k, v.K("title"))
	}

	return nil
}
Exemple #4
0
func main() {

	url := "http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&sensor=false"

	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}

	if resp.StatusCode != http.StatusOK {
		panic(resp.StatusCode)
	}

	json, err := easyjson.NewEasyJson(resp.Body)
	if err != nil {
		panic("json convert err")
	}

	//easy access!
	json.K("routes", 0, "bounds", "southwest").PrettyPrint()

	//support method chain
	json.K("routes").K(0).K("bounds").K("southwest").PrettyPrint()

	for k, v := range json.K("routes").K(0).RangeObjects() {
		fmt.Printf("%v:%v\n", k, v)
	}

	//string value
	copyrights, err := json.K("routes").K(0).K("copyrights").AsString()
	if err != nil {
		panic("AsString err")
	}
	fmt.Printf("copyrights=%s", copyrights)

}