2016-04-22 20:14
浏览 53

为什么我无法使用golang解码此JSON? 它总是打印一个空字符串

I have a JSON file on my server that is very simple, just

    "first_name": "John",
    "last_name": "Doe"

I then wrote a golang script to print out the first name:

package main

import (

type Person struct {
    FirstName string `json: "first_name"`
    LastName string `json: "last_name"`

func main() {
    url := "http://myserver.com/test.json"

    res, err := http.Get(url)

    if err != nil {
        fmt.Printf("%s", err)

    defer res.Body.Close()

    var person Person
    dec := json.NewDecoder(res.Body).Decode(&person)

    if dec != nil {
        fmt.Printf("%s", dec)


But if I type go run test.go it always just prints a newline character seemingly.

What am I doing wrong?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题