json - 从将Json字符串转换为Struct解析错误

标签 json parsing go

我无法解析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":"905356870666@c.us","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":"905356870666@c.us","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 - 从将Json字符串转换为Struct解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59468005/

相关文章:

c# - 在类级别配置 Json.NET 序列化设置

json - 使用 Bash 编写 JSON 文件的最有效方法是什么?

excel - 在 VBA Excel 上遇到 XML 阶段问题

javascript - 使用 JQuery 解析数据

java - ANTLR java测试文件无法创建树语法对象

c# - 在 C# 中从 JSON 对象转换为 expando 对象

php - 使用 JSON 插入时出现问题

go - 如何解决golang中 "missing Location in call to Date"的错误

arrays - 响应为空结构

go - 使用 pg.Array 时如何将 reflect.Pointer() 转换为 []string?