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

版权声明:本文为原创文章,版权归 吾爱博客 所有,转载请联系博主获得授权!
本文地址:https://www.52bd.net/code/180.html

发表评论

正在加载 Emoji
×