首頁 >後端開發 >Golang >解碼/解組複雜的巢狀 JSON 資料結構

解碼/解組複雜的巢狀 JSON 資料結構

WBOY
WBOY轉載
2024-02-10 13:57:091044瀏覽

解码/解组复杂的嵌套 JSON 数据结构

php小編草莓向大家介紹一個強大的技巧,即解碼/解組複雜的嵌套 JSON 資料結構。在現代的網路應用中,JSON 資料結構被廣泛使用,但是當資料結構變得複雜時,解析和提取所需資訊會變得困難。本文將分享一些實用的技巧和方法,幫助大家輕鬆應付這個挑戰。無論您是初學者還是有經驗的開發者,本文都將為您提供有價值的知識和實用的技巧。讓我們一起來解碼和解組複雜的巢狀 JSON 資料結構吧!

問題內容

我正在嘗試處理從 API 傳回的一些 JSON 資料。然而,它並不完全是一個簡單的結構。它有一堆嵌套的鍵/值。我想將其解組為 Go 結構。

使用 go 有簡單的方法嗎?

{
    "http_buffered_trace": {
     "request": {
      "headers": [
       {
        "key": ":authority",
        "value": "server.localhost:9443",
        "raw_value": ""
       },
       {
        "key": ":path",
        "value": "/get_data",
        "raw_value": ""
       },
       {
        "key": ":method",
        "value": "POST",
        "raw_value": ""
       },
       {
        "key": ":scheme",
        "value": "https",
        "raw_value": ""
       },
       {
        "key": "user-agent",
        "value": "curl/8.1.2",
        "raw_value": ""
       },
       {
        "key": "content-length",
        "value": "1059",
        "raw_value": ""
       },
       {
        "key": "accept",
        "value": "application/json",
        "raw_value": ""
       },
       {
        "key": "authorization",
        "value": "auth data here ! ",
        "raw_value": ""
       },
       {
        "key": "content-type",
        "value": "application/json",
        "raw_value": ""
       },
       {
        "key": "x-forwarded-for",
        "value": "10.42.0.1",
        "raw_value": ""
       },
       {
        "key": "x-forwarded-host",
        "value": "server.localhost:9443",
        "raw_value": ""
       },
       {
        "key": "x-forwarded-port",
        "value": "9443",
        "raw_value": ""
       },
       {
        "key": "x-forwarded-proto",
        "value": "https",
        "raw_value": ""
       },
       {
        "key": "x-forwarded-server",
        "value": "traefik-64f55bb67d-6bbjl",
        "raw_value": ""
       },
       {
        "key": "x-real-ip",
        "value": "10.42.0.1",
        "raw_value": ""
       },
       {
        "key": "accept-encoding",
        "value": "gzip",
        "raw_value": ""
       },
       {
        "key": "x-apache-internal",
        "value": "true",
        "raw_value": ""
       },
       {
        "key": "x-request-id",
        "value": "995f0028-432e-499b-8bb9-c102834e150d",
        "raw_value": ""
       },
       {
        "key": "x-apache-expected-rq-timeout-ms",
        "value": "15000",
        "raw_value": ""
       }
      ],
      "body": {
       "as_string": "Request body in here !",
       "truncated": false
      },
      "trailers": []
     },
     "response": {
      "headers": [
       {
        "key": ":status",
        "value": "200",
        "raw_value": ""
       },
       {
        "key": "content-type",
        "value": "application/json; charset=UTF-8",
        "raw_value": ""
       },
       {
        "key": "date",
        "value": "Wed, 01 Nov 2023 13:36:05 GMT",
        "raw_value": ""
       },
       {
        "key": "x-apache-upstream-service-time",
        "value": "4",
        "raw_value": ""
       },
       {
        "key": "server",
        "value": "envoy",
        "raw_value": ""
       }
      ],
      "body": {
       "as_string": "Response body in here!",
       "truncated": false
      },
      "trailers": []
     }
    }
   }

解決方法

這裡有一個簡單的方法:

type Header struct {
    Key      string `json:"key"`
    Value    string `json:"value"`
    RawValue string `json:"raw_value"`
}

type Trailer = Header

type Body struct {
    Truncated bool   `json:"truncated"`
    AsString  string `json:"as_string"`
}

type Request struct {
    Headers  []Header  `json:"headers"`
    Body     Body      `json:"body"`
    Trailers []Trailer `json:"trailers"`
}

type Response = Request

type HttpBufferedTrace struct {
    HttpBufferedTrace struct {
        Request  Request  `json:"request"`
        Response Response `json:"response"`
    } `json:"http_buffered_trace"`
}

func main() {

    h := HttpBufferedTrace{}

    json.Unmarshal([]byte(jsonStr), &h)

    fmt.Println(h)
}

https://www.php.cn/link/a98bf9c158d51c9757bd04eb9d2e16f7

以上是解碼/解組複雜的巢狀 JSON 資料結構的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除