Decode/unmarshal complex nested JSON data structures
php editor Strawberry introduces you to a powerful technique, which is to decode/unmarshal complex nested JSON data structures. In modern web applications, JSON data structures are widely used, but when the data structure becomes complex, it becomes difficult to parse and extract the required information. This article will share some practical tips and methods to help you easily cope with this challenge. Whether you are a beginner or an experienced developer, this article will provide you with valuable knowledge and practical tips. Let’s decode and unmarshal complex nested JSON data structures together!
Question content
I'm trying to process some JSON data returned from an API. However, it's not exactly a simple structure. It has a bunch of nested keys/values. I want to unmarshal it into a Go struct.
Is there an easy way to use 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": [] } } }
Solution
Here is a simple method:
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
The above is the detailed content of Decode/unmarshal complex nested JSON data structures. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The article discusses using table-driven tests in Go, a method that uses a table of test cases to test functions with multiple inputs and outcomes. It highlights benefits like improved readability, reduced duplication, scalability, consistency, and a
