json.Marshal的小细节
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
type User struct {
id int
name string
age int
class string
}
// 官网例子
type ColorGroup struct {
ID int
Name string
Colors []string
}
func (this *UserController) Get() {
fmt.Println(this.Ctx.Input.Param(":user"))
a, _ := this.GetInt(":user")
fmt.Println("hello")
fmt.Println(a)
time.Sleep(time.Duration(a) * time.Second)
//this.Ctx.WriteString("hello World")
user := User{
id: 1,
name: "wang",
age: 22,
class: "class1",
}
body, err := json.Marshal(user)
if err != nil {
fmt.Println("Error: ", err)
}
fmt.Println("user: ",string(body))
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("color: ",string(b))
this.Ctx.WriteString(string(body))
}
output
{}
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
上面的user结构体序列化之后为 空 应该修改为:
type User struct {
Id int // 首字母大写
Name string
Age int
Class string
}
必须要field的首字母大写才能成功的解析为json格式的数据,或者tag(field首字母还是必须要大写,不然为 空 ).不然为空.




适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6
binary -> binary_t
Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 21 天前
f3dc4684
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d)
---
updated-dependencies:
- dependency-name: github/codeql-action
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 28 天前
更多推荐
所有评论(0)