GO jsonstr 2 obj

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Project Main.gopackage mainimport ("Encoding/json" "FMT") func main () {FMT. Println (Help ()) b: = []byte (' {' "Title": "Go Language Programming", "Authors": ["Xushiwei", "Hughlv", "Pandaman", "Guaguasong", "Hantuo", " Bertyuan "," Xudaoli "]," Publisher ":" ituring.com.cn "," ispublished ": true," price ": 9.99," Sales ": 1000000} ') var r Interface{}err: = json. Unmarshal (b, &r) fmt. Println ("R =", r, "err =", err, "\ n") Gobook, OK: = R. (map[string]interface{}) if ok {for k, V: = range Gobook {Switch v2 : = V. (type) {case string:fmt. Println (k, ' is String ', V2) case int:fmt. Println (K, "is Int.", v2) case bool:fmt. Println (k, "is bool", v2) case []INTERFACE{}:FMT. Println (K, "is a array:") for I, IV: = Range v2 {fmt. Println (i, iv)}default:fmt. Println (K, "is another type not handle yet")}}}}func help () The flexible type system built into the string {return ' go ', conveys a valuable message to us: An empty interface is a generic type. If you want to decode the JSON of an unknown structure, simply decode the JSON data to an empty interface. In the process of decoding JSON data, the element types inside the JSON data are converted as follows: The Boolean value in JSON will be converted to the bool type in go, the value will be converted to the float64 type in go, the string is converted to a string type, and the JSON array will be converted to [] interface{} type; JSON objectwill be converted to map[string]interface{} type; null value will be converted to nil '} 

Output:

Go内建这样灵活的类型系统,向我们传达了一个很有价值的信息:空接口是通用类型。如果要解码一段未知结构的JSON,只需将这段JSON数据解码输出到一个空接口即可。在解码JSON数据的过程中, JSON数据里边的元素类型将做如下转换: JSON中的布尔值将会转换为Go中的bool类型; 数值会被转换为Go中的float64类型; 字符串转换后还是string类型; JSON数组会转换为[]interface{}类型; JSON对象会转换为map[string]interface{}类型; null值会转换为nilr =  map[IsPublished:true Price:9.99 Sales:1e+06 Title:Go语言编程 Authors:[XuShiwei HughLv Pandaman GuaguaSong HanTuo BertYuan XuDaoli] Publisher:ituring.com.cn] err =  <nil> Title is string Go语言编程Authors is an array:0 XuShiwei1 HughLv2 Pandaman3 GuaguaSong4 HanTuo5 BertYuan6 XuDaoliPublisher is string ituring.com.cnIsPublished is bool truePrice is another type not handle yetSales is another type not handle yet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.