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\n", val1, reflect.TypeOf(val1)) // 10, float64 i["key"] = int(i["key"].(float64))
val2 := i["key"]
fmt.Printf("%v, %v\n", val2, reflect.TypeOf(val2)) // 10, int

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的更多相关文章

  1. 算法(第四版)C# 习题题解——1.3

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  2. PAT甲级——1025 PAT Ranking

    1025 PAT Ranking Programming Ability Test (PAT) is organized by the College of Computer Science and ...

  3. sqoop sample code

    本文使用的数据库是mysql的sample database employees. download url:https://launchpad.net/test-db/employees-db-1/ ...

  4. Linux Rootkit Sample && Rootkit Defenser Analysis

    目录 . 引言 . LRK5 Rootkit . knark Rootkit . Suckit(super user control kit) . adore-ng . WNPS . Sample R ...

  5. Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)

    本博前面的文章介绍了Android开发环境的搭建和模拟器的常用操作.本次,将以Android Sample中经典的SoftKeyboard项目为例,详细解析Android上一个小型项目的开发过程和注意 ...

  6. Memcached Java Client with sample program--reference

    In my previous post, I listed down most common telnet commands for memcached with sample execution t ...

  7. Misha and Changing Handles

    Description Misha hacked the Codeforces site. Then he decided to let all the users change their hand ...

  8. Windows环境搭建与第一个C# Sample

    Redis入门 - Windows环境搭建与第一个C# Sample   什么是Redis? Redis是一个开源.支持网络.基于内存.键值对存储数据库,使用ANSI C编写.从2013年5月开始,R ...

  9. 关于乱序(shuffle)与随机采样(sample)的一点探究

    最近一个月的时间,基本上都在加班加点的写业务,在写代码的时候,也遇到了一个有趣的问题,值得记录一下. 简单来说,需求是从一个字典(python dict)中随机选出K个满足条件的key.代码如下(py ...

随机推荐

  1. js二维数组转一维数组

    方法一 利用es5的arr.reduce(callback[, initialValue])实现 var arr1 = [[0, 1], [2, 3], [4, 5]]; var arr2 = arr ...

  2. springboot2.0集成webSocket

    WebSocket和http的区别? http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能发送信息. http链接分为短链接,长链接,短链接是每次请求都要三 ...

  3. jeesite直接登录——真实破解

    前台 后台 @RequiresPermissions("alarm:alarm:view")一定要注释 —————————————————————————————————————— ...

  4. Centos搭建hexo教程

    hexo文档:https://hexo.io/zh-cn/ 1.安装Git # sudo yum install git-core// 查看版本# git version// 输出git versio ...

  5. mysql使用存储过程,批量生成测试数据

    1.存储过程代码 delimiter $$DROP PROCEDURE IF EXISTS create_service_data$$create procedure create_service_d ...

  6. mysql的锁机制,以及乐观锁,悲观锁,以及热点账户余额问题

    mysql的简单锁机制. myisam 1.只支持表级锁,所以经常更新的表结构不适宜用. 2.select也会产生锁表 innodb 1.支持事务,行级锁,表级锁,执行行级锁的前提是sql语句的索引有 ...

  7. Web开发的分层结构与MVC模式

    1.分层结构 所谓分层结构.把不同的功能代码封装成类,把相同功能的类封装在一个个的包中,也叫层.功能归类如下: 实体类: 封装数据,是数据的载体,在层与层之间进行传递,数据也就传递了.比如说要传递学生 ...

  8. docker镜像pull不下来最终解决方法

    pull镜像wordpress下来,但是出现如下错误: # docker pull wordpress:latest Error response from daemon: Get https://r ...

  9. Asp.Net Zero轻量级审核流设计

    复杂的业务系统中往往会集成工作流或审核流,但有些轻量及的业务系统对这些功能的需求并不大,有的系统甚至只需要审核功能就够了.这里给大家介绍在Asp.Net Zero中通用轻量及审核流设计,功能具备审核权 ...

  10. Java语言基础(10)

    1 方法(三) 案例:Demo1 import java.util.Scanner; public class Demo1 { static int min(int num1,int num2){ i ...