zoukankan      html  css  js  c++  java
  • go json解析Marshal和Unmarshal

    Decoder:

    package main
    import (
        "encoding/json"
        "fmt"
        "io"
        "log"
        "strings"
    )
    func main ( ) {
        const jsonStream = `
            { "Name" : "Ed" , "Text" : "Knock knock." }
            { "Name" : "Sam" , "Text" : "Who's there?" }
            { "Name" : "Ed" , "Text" : "Go fmt." }
            { "Name" : "Sam" , "Text" : "Go fmt who?" }
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" }
        `
        type Message struct {
            Name , Text string
        }
        dec := json. NewDecoder ( strings. NewReader ( jsonStream ) )
        for {
            var m Message
            if err := dec. Decode ( & m ) ; err == io. EOF {
                break
            } else if err != nil {
                log . Fatal ( err )
            }
            fmt. Printf ( "%s: %s 
     " , m. Name , m. Text )
        }
    }
    

    Marshal:

    package main
    import (
        "encoding/json"
        "fmt"
        "os"
    )
    func main ( ) {
        type ColorGroup struct {
            ID     int
            Name   string
            Colors [ ] string
        }
        group := ColorGroup {
            ID :     1 ,
            Name :   "Reds" ,
            Colors : [ ] string { "Crimson" , "Red" , "Ruby" , "Maroon" } ,
        }
        b , err := json. Marshal ( group )
        if err != nil {
            fmt. Println ( "error:" , err )
        }
        os. Stdout . Write ( b )
    }
    
    

    RawMessage :

    package main
    import (
        "encoding/json"
        "fmt"
        "log"
    )
    func main ( ) {
        type Color struct {
            Space string
            Point json. RawMessage // delay parsing until we know the color space
        }
        type RGB struct {
            R uint8
            G uint8
            B uint8
        }
        type YCbCr struct {
            Y   uint8
            Cb int8
            Cr int8
        }
        var j = [ ] byte ( ` [
            { "Space" : "YCbCr" , "Point" : { "Y" : 255 , "Cb" : 0 , "Cr" : - 10 } } ,
            { "Space" : "RGB" ,   "Point" : { "R" : 98 , "G" : 218 , "B" : 255 } }
        ] ` )
        var colors [ ] Color
        err := json. Unmarshal ( j , & colors )
        if err != nil {
            log . Fatalln ( "error:" , err )
        }
        for _ , c := range colors {
            var dst interface { }
            switch c. Space {
            case "RGB" :
                dst = new ( RGB )
            case "YCbCr" :
                dst = new ( YCbCr )
            }
            err := json. Unmarshal ( c. Point , dst )
            if err != nil {
                log . Fatalln ( "error:" , err )
            }
            fmt. Println ( c. Space , dst )
        }
    }
    
    

    Unmarshal:

    package main
    import (
        "encoding/json"
        "fmt"
    )
    func main ( ) {
        var jsonBlob = [ ] byte ( ` [
            { "Name" : "Platypus" , "Order" : "Monotremata" } ,
            { "Name" : "Quoll" ,     "Order" : "Dasyuromorphia" }
        ] ` )
        type Animal struct {
            Name  string
            Order string
        }
        var animals [ ] Animal
        err := json. Unmarshal ( jsonBlob , & animals )
        if err != nil {
            fmt. Println ( "error:" , err )
        }
        fmt. Printf ( "%+v" , animals )
    }
    
    
  • 相关阅读:
    错误提示:Wrong Local header signature: 0xE011CFD0
    “CreateRiaClientFilesTask”任务意外失败。 未能加载文件程序集“System.ComponentModel.DataAnnot...
    jquery tagsinput监听输入、修改、删除事件
    bootstrap之常见组件应用1
    多啦A梦的制作
    Java 多线程
    Java集合的复习
    10种排序算法分析
    C++ 中vector的学习
    android学习流程确立
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/11498544.html
Copyright © 2011-2022 走看看