golang json判断类型

本文介绍了一种在Go语言中判断JSON字段类型的实用方法,通过示例展示了如何区分字符串和数值类型,这对于处理复杂的JSON数据结构尤其有用。

json怎么判断类型

	if q.Number == '0' {
		fmt.Println("q.Number is string!Pass" )
	}
	if q.Number == 0 {
		fmt.Println("q.Number is not string!Wrong" )
	}

看上去很粗暴但是很实用,并没有查到满意的方法,待补充。

package main
import (
	"encoding/json"
	"fmt"
)

//type Product struct {
//	Name      string
//	ProductID int64
//	Number    int
//	Price     float64
//	IsOnSale  bool
//}
// Product _
type Product struct {
	Name      string  `json:"name"`
	ProductID int64   `json:"-"` // 表示不进行序列化
	Number    int     `json:"number, string"`
	Price     float64 `json:"price, int"`
	IsOnSale  bool    `json:"_is_on_sale"`
}

var test struct {
	t interface{}
}

func main() {
	p := &Product{}

	p.Name = "Hodge"
	p.IsOnSale = false
	p.Number = 0
	p.Price = 0.00
	p.ProductID = 3
	data, _ := json.Marshal(p)

	fmt.Println(string(data))// {"Name":"Hodge","ProductID":3,"Number":1,"Price":2,"IsOnSale":true}


	//t := test{}
	q := &Product{}
	json.Unmarshal([]byte(data), q)
	fmt.Println(*q)

	if q.Number == '0' {
		fmt.Println("q.Number is string!Pass" )
	}
	if q.Number == 0 {
		fmt.Println("q.Number is not string!Wrong" )
	}
}

遍历 JSON 可以使用 Go 语言内置的 encoding/json 包。 假设有以下 JSON 数据: ```json { "name": "John", "age": 30, "email": "john@example.com", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" }, { "type": "work", "number": "555-5678" } ] } ``` 可以使用以下代码遍历该 JSON: ```go package main import ( "encoding/json" "fmt" ) func main() { jsonData := `{ "name": "John", "age": 30, "email": "john@example.com", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" }, { "type": "work", "number": "555-5678" } ] }` var result map[string]interface{} err := json.Unmarshal([]byte(jsonData), &result) if err != nil { fmt.Println("error:", err) } for key, value := range result { switch valueType := value.(type) { case string: fmt.Println(key, "is string:", valueType) case float64: fmt.Println(key, "is float64:", valueType) case map[string]interface{}: fmt.Println(key, "is an object:") for subKey, subValue := range valueType { fmt.Println(subKey, ":", subValue) } case []interface{}: fmt.Println(key, "is an array:") for i, arrValue := range valueType { fmt.Println(i, ":", arrValue) } default: fmt.Println(key, "is of a different type") } } } ``` 输出结果为: ``` name is string: John age is float64: 30 email is string: john@example.com address is an object: street : 123 Main St city : New York state : NY zip : 10001 phoneNumbers is an array: 0 : map[number:555-1234 type:home] 1 : map[number:555-5678 type:work] ``` 可以看到,遍历 JSON 数据需要使用类型断言,判断当前值的类型,并根据类型进行相应操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值