第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Golang - 解析嵌套的 json 值

Golang - 解析嵌套的 json 值

Go
慕村225694 2022-06-01 15:10:48
我有一個(gè)看起來(lái)像這樣的 json 數(shù)據(jù):    [      {        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],             ]          ]        }     }   ]我想解析這些數(shù)據(jù),從這個(gè)數(shù)組中獲取第一個(gè)元素并將其轉(zhuǎn)換為一個(gè)新的結(jié)構(gòu),如下所示:type Location struct {    Name        string    Country     string    CountryCode string    Center      Coordinate    Coordinates []Coordinate}我已經(jīng)定義了這樣的坐標(biāo)類型: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)但是,這在終端中給了我這個(gè):[{   { } []}]如何解析這種json結(jié)構(gòu)并將其轉(zhuǎn)換為location那種結(jié)構(gòu)?
查看完整描述

2 回答

?
滄海一幻覺(jué)

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

您應(yīng)該使用與輸入文檔匹配的結(jié)構(gòu)來(lái)解組:


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"`

}

然后解組為一個(gè)條目數(shù)組:


var entries []Entry

json.Unmarshal(data,&entries)

并且,用于entries構(gòu)造Locations


查看完整回答
反對(duì) 回復(fù) 2022-06-01
?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

您需要對(duì) Unmarshal 的結(jié)構(gòu)更加精確。根據(jù)官方 JSON 規(guī)范,您還需要在密鑰周圍加上雙引號(hào)。省略引號(hào)僅適用于 JavaScript,JSON 需要這些。


最后一件事,我知道這很愚蠢,但是最后一個(gè)內(nèi)部數(shù)組之后的最后一個(gè)逗號(hào)也是無(wú)效的,必須刪除它:


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)

}


查看完整回答
反對(duì) 回復(fù) 2022-06-01
  • 2 回答
  • 0 關(guān)注
  • 295 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)