在Go中使用JSON时出现恐慌

huangapple go评论81阅读模式
英文:

panic with JSON in go

问题

我尝试运行以下代码

package main

import (
    "encoding/json"
    "fmt"
    /*"labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"*/
)

func insertEntry(j *map[string]interface{}, entry string) {
    err := json.Unmarshal([]byte(entry), j)
    if err != nil {
        panic(err)
    }

}

func main() {
    c1 := "{" +
        `"mw" : 42.0922,` +
        `"ΔfH°gas" : {` +
        `   "value" : 372.38,` +
        `   "units" : "kJ/mol"` +
        `},` +
        `"S°gas" : {` +
        `   "value" : 216.81,` +
        `   "units" : "J/mol×K"` +
        `},` +
        `"index" : [` +
        `   {"name" : "mw", "value" : 42.0922},` +
        `   {"name" : "ΔfH°gas", "value" : 372.38},` +
        `   {"name" : "S°gas", "value" : 216.81}` +
        `]` +
        `}`

    c2 := "{" +
        `"name" : "silicon",` +
        `"mw" : 32.1173,` +
        `}` +
        `"index" : [` +
        `   {"name" : "mw", "value" : 32.1173}` +
        `]` +
        `}`

    var m map[string]interface{}

    insertEntry(&m, c1)
    insertEntry(&m, c2)
    chemical := m["ΔfH°gas"].(map[string]interface{})
    fmt.Println("value: %s\n", chemical["value"].(string))
    fmt.Println("units: %s\n", chemical["units"].(string))

But I got the following error message:

    $ go run chemeo.go 
    panic: invalid character '}' looking for beginning of object key string

    goroutine 1 [running]:
    main.insertEntry(0xf840045100, 0x4badc4, 0x7f5e00000056, 0x20043115c)
            /media/mictadlo/projects/mgo/chemeo/chemeo.go:19 +0xd8
    main.main()
            /media/mictadlo/projects/mgo/chemeo/chemeo.go:54 +0xa3

    goroutine 2 [syscall]:
    created by runtime.main
            /usr/local/go/src/pkg/runtime/proc.c:221
    exit status 2

What did I do wrong?

英文:

I tried to run the following code

package main

import (
    "encoding/json"
    "fmt"
    /*"labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"*/
)

func insertEntry(j *map[string]interface{}, entry string) {
    err := json.Unmarshal([]byte(entry), j)
    if err != nil {
	    panic(err)
    }

}

func main() {
    c1 := "{" +
	    `"mw" : 42.0922,` +
	    `"ΔfH°gas" : {` +
	    `   "value" : 372.38,` +
	    `   "units" : "kJ/mol"` +
	    `},` +
	    `"S°gas" : {` +
	    `   "value" : 216.81,` +
	    `   "units" : "J/mol×K"` +
	    `},` +
	    `"index" : [` +
	    `   {"name" : "mw", "value" : 42.0922},` +
	    `   {"name" : "ΔfH°gas", "value" : 372.38},` +
	    `   {"name" : "S°gas", "value" : 216.81}` +
	    `]` +
	    `}`

    c2 := "{" +
	    `"name" : "silicon",` +
	    `"mw" : 32.1173,` +
	    `}` +
	    `"index" : [` +
	    `   {"name" : "mw", "value" : 32.1173}` +
	    `]` +
	    `}`

    var m map[string]interface{}

    insertEntry(&m, c1)
    insertEntry(&m, c2)
    chemical := m["ΔfH°gas"].(map[string]interface{})
    fmt.Println("value: %s\n", chemical["value"].(string))
    fmt.Println("units: %s\n", chemical["units"].(string))

But I got the following error message:

    $ go run chemeo.go 
    panic: invalid character '}' looking for beginning of object key string

    goroutine 1 [running]:
    main.insertEntry(0xf840045100, 0x4badc4, 0x7f5e00000056, 0x20043115c)
            /media/mictadlo/projects/mgo/chemeo/chemeo.go:19 +0xd8
    main.main()
            /media/mictadlo/projects/mgo/chemeo/chemeo.go:54 +0xa3

    goroutine 2 [syscall]:
    created by runtime.main
            /usr/local/go/src/pkg/runtime/proc.c:221
    exit status 2

What did I do wrong?

答案1

得分: 12

你的c2变量保存的是无效的JSON:

c2 := "{" +
`"name" : "silicon",` +
`"mw" : 32.1173,` +
`}` +
`"index" : [` +
` {"name" : "mw", "value" : 32.1173}` +
`]` +
`}`

清理后,它应该是这样的:

c2 := `{
    "name" : "silicon",
    "mw" : 32.1173,
}
"index" : [
    {"name" : "mw", "value" : 32.1173}
]
}`

你可以看到中间多了一个}

它应该是这样的:

c2 := `{
    "name": "silicon",
    "mw": 32.1173,
    "index": [
        {
            "name": "mw",
            "value": 32.1173
        }
    ]
}`
英文:

Your c2 variable is holding invalid JSON:

c2 := "{" +
`"name" : "silicon",` +
`"mw" : 32.1173,` +
`}` +
`"index" : [` +
` {"name" : "mw", "value" : 32.1173}` +
`]` +
`}`

Cleaned up, it'll look like this:

c2 := `{
    "name" : "silicon",
    "mw" : 32.1173,
}
"index" : [
    {"name" : "mw", "value" : 32.1173}
]
}`

You can see there's an extra } in the middle.

It should look like this:

c2 := `{
    "name": "silicon",
    "mw": 32.1173,
    "index": [
        {
            "name": "mw",
            "value": 32.1173
        }
    ]
}`

huangapple
  • 本文由 发表于 2013年3月14日 07:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/15398559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定