登录
首页 >  Golang >  Go问答

出现解析错误时将Json字符串转换为结构

来源:stackoverflow

时间:2024-02-26 09:00:24 127浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《出现解析错误时将Json字符串转换为结构》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

我无法解析我发送演示链接的 json 值 对此有什么想法吗?这是链接和代码

https://play.golang.org/p/qhzps_-618s

package main

import (
    "encoding/json"
    "fmt"
    //mapstructure "github.com/mitchellh/mapstructure"

)

type presence struct{
    id string 
    m_type string 
    deny string 
}
type jsonHandler struct {
    name string 
    dat map[string]interface{}

}   

func main() {
    s := `["Presence",{"id":"[email protected]","type":"unavailable","deny":true}]`
    data := jsonHandler{}
    json.Unmarshal([]byte(s), &data)
    fmt.Printf("Operation: %s", data.name)


}

输出: 手术: 程序退出。


解决方案


尝试使用这个:https://play.golang.com/p/UICf_uNNFdC

为了增强代码的可读性,我添加了很多注释。请务必正确处理错误并删除调试打印。

package main

import (
    "encoding/json"
    "log"
    "strings"
)

type Presence struct {
    Presence string
    ID       string `json:"id"`
    Type     string `json:"type"`
    Deny     bool   `json:"deny"`
}

type JsonHandler struct {
    Name string   `json:"name"`
    Dat  Presence `json:"dat"`
}

func main() {
    var (
        // Used for unmarshal a given json
        packedData []json.RawMessage
        err        error
        // Data that does not have a related json key
        name []byte
        // Used for extract the raw data that will be unmarshalled into the Presence struct
        temp []byte
        // Nested json
        jsonPresence Presence
        handler      JsonHandler
    )

    s := `["Presence",{"id":"[email protected]","type":"unavailable","deny":true}]`

    log.Println("Dealing with -> " + s)

    // Unmarshall into a raw json message
    err = json.Unmarshal([]byte(s), &packedData)
    if err != nil {
        panic(err)
    }

    // Extract the presence
    log.Println("Presence: ", string(packedData[0]))
    // Extract the nested json
    log.Println("Packed: ", string(packedData[1]))

    // NOTE: 0 refers to the first value of the JSON
    name, err = packedData[0].MarshalJSON()
    if err != nil {
        panic(err)
    }
    log.Println("Value that does not have a key: " + string(name))
    handler.Name = strings.Replace(string(name), "\"", "", -1)

    // NOTE: 1 refers to the second value of the JSON, the entire JSON
    // Unmarshal the nested Json into byte
    temp, err = packedData[1].MarshalJSON()
    if err != nil {
        panic(err)
    }

    // Unmarshal the raw byte into the struct
    err = json.Unmarshal(temp, &jsonPresence)
    if err != nil {
        panic(err)
    }

    log.Println("ID:", jsonPresence.ID)
    log.Println("Type:", jsonPresence.Type)
    log.Println("Deny:", jsonPresence.Deny)

    handler.Dat = jsonPresence

    log.Println("Data unmarshalled: ", handler)
}

终于介绍完啦!小伙伴们,这篇关于《出现解析错误时将Json字符串转换为结构》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>