亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang - 解析嵌套的 json 值

Golang - 解析嵌套的 json 值

Go
慕村225694 2022-06-01 15:10:48
我有一個看起來像這樣的 json 數據:    [      {        lat: "41.189301799999996",        lon: "11.918255998031015",        display_name: "Some place",        address: {          address: "Address",          country: "Country",          country_code: "CC"        },        geojson: {          type: "Polygon",          coordinates: [             [               [14.4899021,41.4867039],               [14.5899021,41.5867039],             ]          ]        }     }   ]我想解析這些數據,從這個數組中獲取第一個元素并將其轉換為一個新的結構,如下所示:type Location struct {    Name        string    Country     string    CountryCode string    Center      Coordinate    Coordinates []Coordinate}我已經定義了這樣的坐標類型:type Coordinate struct {    Lat string `json:"lat"`    Lng string `json:"lon"`}但是,如果嘗試這樣解析:bytes, err := ioutil.ReadAll(res.Body)if err != nil {    http.Error(w, err.Error(), http.StatusBadRequest)}var locations [0]Locationif err := json.Unmarshal(bytes, &locations); err != nil {    fmt.Println("Error parsing json", err)}fmt.Println(locations)但是,這在終端中給了我這個:[{   { } []}]如何解析這種json結構并將其轉換為location那種結構?
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

您應該使用與輸入文檔匹配的結構來解組:


type Entry struct {

  Lat string `json:"lat"`

  Lon string `json:"lon"`

  DisplayName string `json:"display_name"`

  Address struct {

      Address string `json:"address"`

      Country string `json:"country"`

      Code string `json:"country_code"`

  } `json:"address"`

  Geo struct {

     Type string `json:"type"`

     Coordinates [][][]float64 `json:"coordinates"`

  } `json:"geojson"`

}

然后解組為一個條目數組:


var entries []Entry

json.Unmarshal(data,&entries)

并且,用于entries構造Locations


查看完整回答
反對 回復 2022-06-01
?
MM們

TA貢獻1886條經驗 獲得超2個贊

您需要對 Unmarshal 的結構更加精確。根據官方 JSON 規范,您還需要在密鑰周圍加上雙引號。省略引號僅適用于 JavaScript,JSON 需要這些。


最后一件事,我知道這很愚蠢,但是最后一個內部數組之后的最后一個逗號也是無效的,必須刪除它:


package main


import (

    "encoding/json"

    "fmt"

)


type Location struct {

    Lat         string     `json:"lat"`

    Lng         string     `json:"lon"`

    DisplayName string     `json:"display_name"`

    Address     Address    `json:"address"`

    GeoJSON     Geo        `json:"geojson"`

}


type Address struct {

    Address     string `json:"address"`

    Country     string `json:"country"`

    CountryCode string `json:"country_code"`

}


type Geo struct {

    Type        string         `json:"type"`

    Coordinates [][]Coordinate `json:"coordinates"`

}


type Coordinate [2]float64


func main() {

    inputJSON := `

    [

          {

            "lat": "41.189301799999996",

            "lon": "11.918255998031015",

            "display_name": "Some place",

            "address": {

              "address": "123 Main St.",

              "country": "USA",

              "country_code": "+1"

            },

            "geojson": {

              "type": "Polygon",

              "coordinates": [

                [

                  [14.4899021,41.4867039],

                  [14.5899021,41.5867039]

                ]

              ]

            }

          }

        ]`


    var locations []Location


    if err := json.Unmarshal([]byte(inputJSON), &locations); err != nil {

        panic(err)

    }


    fmt.Printf("%#v\n", locations)

}


查看完整回答
反對 回復 2022-06-01
  • 2 回答
  • 0 關注
  • 290 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號