Golang-解析嵌套的json值
我有一个json数据,如下所示:Golang-解析嵌套的json值,json,go,Json,Go,我有一个json数据,如下所示: [ { lat: "41.189301799999996", lon: "11.918255998031015", display_name: "Some place", address: { address: "Address", country: "Country", country_code: "CC"
[
{
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]Location
if err := json.Unmarshal(bytes, &locations); err != nil {
fmt.Println("Error parsing json", err)
}
fmt.Println(locations)
我定义了如下坐标类型:
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]Location
if err := json.Unmarshal(bytes, &locations); err != nil {
fmt.Println("Error parsing json", err)
}
fmt.Println(locations)
但是,如果尝试这样分析:
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]Location
if err := json.Unmarshal(bytes, &locations); err != nil {
fmt.Println("Error parsing json", err)
}
fmt.Println(locations)
但是,这给了我一个终端:
[{ { } []}]
如何解析这种json结构并将其转换为
位置类型的结构?您应该使用与要解组的输入文档匹配的结构:
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)
并且,使用条目
构造位置
s您应该使用与输入文档匹配的结构来解组:
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)
并且,使用条目
来构造位置
s您需要更精确地处理要解组的结构。根据官方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)
}
你也可以 您需要更精确地处理您要解组的结构。根据官方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)
}
你也可以 您应该定义一个与JSON输入相匹配的结构,并将其解组,然后从解组的值手动构造Location结构。在本例中,与嵌套值相匹配的结构是什么样子的,国家代码?您应该定义一个与JSON输入相匹配的结构,将其解组,并从未组分的值手动构造Location结构。在本例中,匹配嵌套值的结构与国家代码是什么样的?