zoukankan      html  css  js  c++  java
  • This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa

    This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}.

    When it did unmarshal using map[string]interface{}, a number with “int” was changed to “float64”. And it shows an error as follows.

    Error :

    panic: interface conversion: interface {} is float64, not int

    Sample Script : It solves using following script.

    package main
    
    import (
        "encoding/json"
        "fmt"
        "reflect"
    )
    
    func main() {
        data := `{"key": 10}`
        var i map[string]interface{}
        json.Unmarshal([]byte(data), &i)
    
        val1 := i["key"]
        fmt.Printf("%v, %v
    ", val1, reflect.TypeOf(val1)) // 10, float64
    
        i["key"] = int(i["key"].(float64))
        val2 := i["key"]
        fmt.Printf("%v, %v
    ", val2, reflect.TypeOf(val2)) // 10, int
  • 相关阅读:
    Oracle-函数
    Oracle-存储过程
    Linux-文件系统概述
    Oralce-PL/SQL编程-游标
    Oracle -操作数据库
    Oralce常用系统函数
    SQL语言基础-基本概念
    Linux-进程管理
    Linux-用户管理
    shell里的IFS内置环境变量
  • 原文地址:https://www.cnblogs.com/cjjjj/p/11712657.html
Copyright © 2011-2022 走看看