2017年2月23日 星期四

[golang] 使用Golang 抓取LASS JSON API的資訊



LASSjson api2層的json結構,網路搜尋到的json範例,多是只能抓取一層,能抓取第2層的例子,卻只能在第2層放內建的intfloat之類的資料型態,因LASS2層資料用golangjson讀取後會形成一個map的資料結構,所以須使用interface陣列才能進去讀取第2層。

num_of_records這個欄位轉換後的資料型態是float64可以直接印出來但如果要用於迴圈變數須轉成整數n=int(m["num_of_records"].(float64))
以下是程式碼。(保留網路上找來的parseArray()parseMap(),以後可能會用到)

package main

import (
        "encoding/json"
        "fmt"
        "io/ioutil"
        "net/http"
)

func main() {
        resp, err := http.Get("https://data.lass-net.org/data/last-all-lass.json")
        jsonResponse, _ := ioutil.ReadAll(resp.Body)

        var m map[string]interface{}
        if err := json.Unmarshal(jsonResponse, &m); err != nil {
                fmt.Println("Fatal")
                panic(err)
        }

        if err != nil {
                fmt.Println("error: ", err)
        } else {
                //fmt.Println(m)
                fmt.Println("ver=", m["version"])
                fmt.Println("src=", m["source"])
                fmt.Println("num=", m["num_of_records"])
                //fmt.Println("feeds=", m["feeds"].(map)) //how to extract records??
        }
        //parseMap(m)
        //parseArray(m["feeds"].([]interface{}))
        //fmt.Println("feeds[0]=", m["feeds"].([]interface{})[0].(map[string]interface{})["gps_num"])
        //for lassData := range m["feeds"].([]interface{}) {
        //n := m["num_of_records"].(type)
        switch m["num_of_records"].(type) {
        case string:
                fmt.Println("type is string")
        case int:
                fmt.Println("type is int")
        case float32:
                fmt.Println("type is float32")
        case float64:
                fmt.Println("type is float64") //===> this is the answer
        default:
                fmt.Println("type is unknown")
        }

        n := int(m["num_of_records"].(float64)) //n, ok := m["num_of_records"].(int) //bad ?
        fmt.Println("n=", n)

        for i, lassData := range m["feeds"].([]interface{}) {
                fmt.Println("------------ record # ", i, "------------")
                fmt.Print("device_id=", lassData.(map[string]interface{})["device_id"])
                fmt.Print(", SiteName=", lassData.(map[string]interface{})["SiteName"])
                fmt.Print(", gpsLat=", lassData.(map[string]interface{})["gpsLat"])
                fmt.Print(", gpsLon=", lassData.(map[string]interface{})["gpsLon"])
                fmt.Print(", timestamp=", lassData.(map[string]interface{})["timestamp"])
                fmt.Print(", s_d0=", lassData.(map[string]interface{})["s_d0"])
                fmt.Print(", s_d1=", lassData.(map[string]interface{})["s_d1"])
                fmt.Print(", s_d2=", lassData.(map[string]interface{})["s_d2"])
                fmt.Print(", s_h0", lassData.(map[string]interface{})["s_h0"])
                fmt.Println(", s_h1=", lassData.(map[string]interface{})["s_h1"])
                //fmt.Println("gps_num=", lassData.(map[string]interface{})["gps_num"])
        }
}

func parseMap(aMap map[string]interface{}) {
        for key, val := range aMap {
                switch concreteVal := val.(type) {
                case map[string]interface{}:
                        fmt.Println("--- parseMap():map[string]interface{} --------")
                        fmt.Println(key)
                        parseMap(val.(map[string]interface{}))
                case []interface{}:
                        fmt.Println("--- parseMap():[]interface{} --------")
                        fmt.Println(key)
                        parseArray(val.([]interface{}))
                default:
                        fmt.Println("--- parseMap():default --------")
                        fmt.Println(key, ":", concreteVal)
                }
        }
}

func parseArray(anArray []interface{}) {
        for i, val := range anArray {
                switch concreteVal := val.(type) {
                case map[string]interface{}:
                        fmt.Println("--- parseArray():map[string]interface{} --------")
                        fmt.Println("Index:", i)
                        parseMap(val.(map[string]interface{}))
                case []interface{}:
                        fmt.Println("--- parseArray():[]interface{} --------")
                        fmt.Println("Index:", i)
                        parseArray(val.([]interface{}))
                default:
                        fmt.Println("--- parseArray():default --------")
                        fmt.Println("Index", i, ":", concreteVal)

                }
        }
}

沒有留言:

張貼留言