package main

import (
"encoding/json"
"fmt"
"os"
) type ConfigStruct struct {
Host string `json:"host"`
Port int `json:"port"`
AnalyticsFile string `json:"analytics_file"`
StaticFileVersion int `json:"static_file_version"`
StaticDir string `json:"static_dir"`
TemplatesDir string `json:"templates_dir"`
SerTcpSocketHost string `json:"serTcpSocketHost"`
SerTcpSocketPort int `json:"serTcpSocketPort"`
Fruits []string `json:"fruits"`
} type Other struct {
SerTcpSocketHost string `json:"serTcpSocketHost"`
SerTcpSocketPort int `json:"serTcpSocketPort"`
Fruits []string `json:"fruits"`
} func main() {
jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}` //json str 转map
var dat map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
fmt.Println("==============json str 转map=======================")
fmt.Println(dat)
fmt.Println(dat["host"])
} //json str 转struct
var config ConfigStruct
if err := json.Unmarshal([]byte(jsonStr), &config); err == nil {
fmt.Println("================json str 转struct==")
fmt.Println(config)
fmt.Println(config.Host)
} //json str 转struct(部份字段)
var part Other
if err := json.Unmarshal([]byte(jsonStr), &part); err == nil {
fmt.Println("================json str 转struct==")
fmt.Println(part)
fmt.Println(part.SerTcpSocketPort)
} //struct 到json str
if b, err := json.Marshal(config); err == nil {
fmt.Println("================struct 到json str==")
fmt.Println(string(b))
} //map 到json str
fmt.Println("================map 到json str=====================")
enc := json.NewEncoder(os.Stdout)
enc.Encode(dat) //array 到 json str
arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"}
lang, err := json.Marshal(arr)
if err == nil {
fmt.Println("================array 到 json str==")
fmt.Println(string(lang))
} //json 到 []string
var wo []string
if err := json.Unmarshal(lang, &wo); err == nil {
fmt.Println("================json 到 []string==")
fmt.Println(wo)
}
}
//go post方式提交
//导入:"net/http" "crypto/tls" //调用片段: ////machineArry
jsonMachine, err := json.Marshal(machineArry)
if err == nil {
fmt.Println("================array 到 json str==")
fmt.Println(string(jsonMachine))
} //?link_token="+token_updatemachine
token_updatemachine :=beego.AppConfig.String("token_updatemachine") mappara :=map[string]interface{}{}
mappara["updateJson"]=string(jsonMachine)
mappara["link_token"]=token_updatemachine
masterUrl222 := beego.AppConfig.String("masterUrl") url222 := masterUrl222+"/cmdb/cmdbapi/updatemachine"
resmap :=this.HttpSendByJsonPost(url222,mappara) if resmap.(map[string]interface{})["code"].(string) =="000" {
return updatemap
}else{ updatemap["_code"] = "1"
updatemap["_msg"] = resmap.(map[string]interface{})["result"] return updatemap } func (this *BaseController) HttpSendByJsonPost(url string,formData map[string]interface{}) interface{} {
req := httplib.Post(url)
req.SetTransport(&http.Transport{DisableKeepAlives: true})
for k, v := range formData {
req.Param(k, v.(string))
}
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
//req.Header("user", user)
//req.Header("role", role)
//req.SetHost()
var result interface{}
req.ToJson(&result) fmt.Println("resultresultresultresultresult:--------------->>>>>>>",result)
return result
}

golang的json操作的更多相关文章

  1. golang的json操作[转]

    package main   import (     "encoding/json"     "fmt"     "os" )   typ ...

  2. 48 【golang】json的效率

    本文将主要做如下几方面的测试: 1,构造一个[100]struct的数组,然后来测试它的json编码后的字符串 或者([]byte),首先关心它的功能是否正常: 2,在很早之前,我们在使用golang ...

  3. Golang 处理 Json(二):解码

    golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...

  4. Golang的json包

    encoding/json encoding/json是官方提供的标准json, 实现RFC 7159中定义的JSON编码和解码.使用的时候需要预定义struct,原理是通过reflection和in ...

  5. Golang解析json的几种方法

    Golang解析json的几种方法 概要 使用Golang调用其它平台API接口时总会被多层的json串给恶心到,我记录一下自己解析json的几种方法. 一.自带的json包 func JsonUnm ...

  6. Json操作(DynamicJson)

    Json的简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了 ...

  7. ASP.NET 里的 JSON操作

    最近项目中需要用到 JSON操作,google了一下 找到了几个比较好的操作方法.... 一 .使用 mircosoft 提供的 .NET Framework 自带的 json操作方法 1. 使用Ja ...

  8. Newtonsoft.Json 操作 JSON 字符串

    Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataContractJsonSeriali ...

  9. Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类

    本文目录 1.  前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...

随机推荐

  1. app 摇一摇功能

    1.重写canBecomeFirstResponder ,并返回YES -(BOOL)canBecomeFirstResponder{ return YES; } 2.重写UIResponder的三个 ...

  2. 【项目】iOS - 使用UIWebView占用内存过大

    通过其他博主介绍的解决这个问题的博客: http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part1-memory-leak ...

  3. Cheminformatic Set

    蛋白: 数据库 1. 蛋白晶体结构数据库 http://www.rcsb.org/pdb/home/home.do 2. 蛋白注释数据库 http://www.uniprot.org/ 工具 1. r ...

  4. java校验时间格式 HH:MM

    package com; import java.text.SimpleDateFormat; import java.util.Date; /** * @author Gerrard */ publ ...

  5. python--文件删除、判断目录存在、字符串替换

    昨晚笔试了金山WPS的测试开发卷,做个笔记(但不是答案,只是我的想法),关于文件和字符串的处理正在写入与完善至上一篇的博客中,现在题目如下: 1.使用脚本语言删除E:\abc目录下的所有文件: 利用o ...

  6. Sublime Text3快捷键汇总

    选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...

  7. JavaScript排序算法——堆排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. JavaWeb学习笔记——Tomcat配置

    使用的Tomcat版本是apache-tomcat-6.0.20 详细的环境变量配置参考<windows 7系统安装与配置Tomcat服务器环境> 网址为http://jingyan.ba ...

  9. windows命令关机

    不知道为啥,远程连接的window服务器没有关机命令,感觉是不是管理员权限导致的,所以找了下用命令关机,如下 shutdown -s -t #5秒内关机 shutdown -r -t #5秒内重启

  10. Xcode如何编译Debug版和Release版

    在Run和Stop按钮的右边有一个工程名 点击工程名,选择Manage Schemes 选择Edit... 左侧选择Run ProjectName.app 右侧选择Info页,在Build Confi ...