Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...