main:

package main

import (
    "encoding/json"
    "fmt"
    // "strings"
    // "strconv"
)
func main(){
    jsonStr := `{
        "deviceType": "CustomCategory",
        "iotId": "s1yfVm0w8HYlx2Zde7Sw000100",
        "requestId": "123",
        "productKey": "a1MRVbH5dTu",
        "gmtCreate": 1585788928824,
        "deviceName": "un00101",
        "items": {
            "Sate09": {
                "value": "19-1111-111--1",
                "time": 1585788928826
            },
            "Sate08": {
                "value": "18-1111-111--1",
                "time": 1585788928826
            },
            "Sate19": {
                "value": "10-1111-111--1",
                "time": 1585788928826
            },
            "Sate07": {
                "value": "17-1111-111--1",
                "time": 1585788928826
            },
            "Sate18": {
                "value": "9-1111-111--1",
                "time": 1585788928826
            },
            "Sate06": {
                "value": "31-1111-111--1",
                "time": 1585788928826
            },
            "Sate17": {
                "value": "8-1111-111--1",
                "time": 1585788928826
            },
            "Sate05": {
                "value": "88-1111-111--1",
                "time": 1585788928826
            },
            "Sate16": {
                "value": "7-1111-111--1",
                "time": 1585788928826
            },
            "Sate04": {
                "value": "24-1111-111--1",
                "time": 1585788928826
            },
            "Sate15": {
                "value": "6-1111-111--1",
                "time": 1585788928826
            },
            "Sate03": {
                "value": "23-0001-000-1585788922",
                "time": 1585788928826
            },
            "Sate25": {
                "value": "16-1111-111--1",
                "time": 1585788928826
            },
            "Sate14": {
                "value": "5-1111-111--1",
                "time": 1585788928826
            },
            "Sate02": {
                "value": "22-1111-111--1",
                "time": 1585788928826
            },
            "Sate24": {
                "value": "15-1111-111--1",
                "time": 1585788928826
            },
            "Sate13": {
                "value": "4-1111-111--1",
                "time": 1585788928826
            },
            "Sate01": {
                "value": "21-1111-111--1",
                "time": 1585788928826
            },
            "Sate23": {
                "value": "14-1111-111--1",
                "time": 1585788928826
            },
            "Sate11": {
                "value": "2-1111-111--1",
                "time": 1585788928826
            },
            "Sate22": {
                "value": "13-1111-111--1",
                "time": 1585788928826
            },
            "Sate10": {
                "value": "20-1111-111--1",
                "time": 1585788928826
            },
            "Sate21": {
                "value": "12-1111-111--1",
                "time": 1585788928826
            },
            "Sate20": {
                "value": "11-1111-111--1",
                "time": 1585788928826
            }
        }
    }`

    var dat map[string]interface{}
    err := json.Unmarshal([]byte(jsonStr), &dat); 
    if err == nil {
        fmt.Println("==============json str 转map=======================")
    items := dat["items"].(map[string]interface{}) //获取 items
        for k := range items{
            val := items[k].(map[string]interface{})["time"]
        fmt.Println(val) //错误的方法
            fmt.Println(int64(val.(float64))) //正确的方法

        }
    }else{
        fmt.Println(err.Error())
    }
}
错误的方法输出:
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
1.585788928826e+12
正确的方法输出:
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826
1585788928826

本文由 吾爱 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论