douqiao7188 2019-04-11 17:04
浏览 19
已采纳

如何在Go lang中解组Json字符串以获得仅一个键的值

So i need to decode a json string to obtain value of key ID but i was not able to find a way to only extract value of only one key so i created a struct of whole json string so that i can unmarshal it to extract info but i think there is some problem with my struct because i don't get any output

package main

import (
    "fmt"
    "encoding/json"
)
type season struct{
    Data []seasoninfo `json:"seasoninfo"`
}
type seasoninfo struct{
    Aliases []string
    Banner string
    FirstAired string
    Id int
    Network string
    Overview string
    SeriesName string
    Slug string
    Status string
}
func main() {
    s := `{"data":[{"aliases":[],"banner":"graphical/81189-g21.jpg","firstAired":"2008-01-20","id":81189,"network":"AMC","overview":"Walter White, a struggling high school chemistry teacher, is diagnosed with advanced lung cancer. He turns to a life of crime, producing and selling methamphetamine accompanied by a former student, Jesse Pinkman, with the aim of securing his family's financial future before he dies.","seriesName":"Breaking Bad","slug":"breaking-bad","status":"Ended"},{"aliases":[],"banner":"","firstAired":"","id":356427,"network":"AMC","overview":null,"seriesName":"Breaking Bad: Original Minisodes","slug":"breaking-bad-original-minisodes","status":"Ended"},{"aliases":["Breaking Bad (ES)"],"banner":"graphical/273859-g.jpg","firstAired":"2014-06-08","id":273859,"network":"Univision","overview":"“Metastasis” is the story of a struggling high school chemistry teacher who is diagnosed with inoperable lung cancer. He turns to a life of crime, producing and selling meth with a former student in order to secure his family’s financial future before he passes away.","seriesName":"Metastasis","slug":"metastasis","status":"Ended"}]}`
    var series season
    err:=json.Unmarshal([]byte(s),&series)
    if err==nil{
        fmt.Println(series)
    } else{
        fmt.Println("wrong")
    }
}

Output is {[]}

https://play.golang.org/p/5jYSp4cMCok

Thanks

  • 写回答

1条回答 默认 最新

  • dougua2309 2019-04-11 17:13
    关注

    The issue is that you are currently trying to match struct field Data to JSON property key seasoninfo. However your sample JSON input doesn't have seasoninfo as the outer property, it has data as an outer property. Either change json:"seasoninfo" to json:"data":

    type season struct{
      Data []seasoninfo `json:"data"`
    }
    

    Here is an an example in action.

    Or even simpler you could consider simply removing the json:"data" portion as data in the JSON matches Data of the struct field:

    Here is an example in action:

    type season struct{
        Data []seasoninfo
    }
    

    Hopefully that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题