Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

go - Golang parse JSON returns 0

The JSON that I am at tempting to parse is very basic and looks like this.

{"id": 3, "title":"Test"}

The following is the code that I am attempting to use for creating and parsing the JSON.

package main

import (
    "fmt"
    "encoding/json"
)

type Config struct{
    id int
    title string
}

func main() {
    var jsonStr = []byte(`{"id": 3, "title":"Test"}`)
    var conf Config
    err := json.Unmarshal(jsonStr, &conf)
    if err!=nil{
            fmt.Print("Error:",err)
    }
    fmt.Println(conf)
    fmt.Println(string(jsonStr))
}

Been looking over a lot of different code examples and can't see what I'm doing wrong. When I attempt to run this, this is what I get as a return.

{0 }
{"id": 3, "title":"Test"} 

I have verified that the JSON is valid, but continue to get an empty return when attempting to use json.Unmarshal. Any ideas on what I am missing so that I can get this JSON parsed?

EDIT: Looks like I can get this to work if I capitalize the titles (Id, Title). Unfortunately the return I am testing for is a return from an API which returns everything in lowercase. I need to be able to parse this JSON with lowercase titles as listed above.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...