> For the complete documentation index, see [llms.txt](https://sec88.0x88.online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sec88.0x88.online/programming/go/package/json.md).

# Json

### Encoding

```go
// Using Maps for aribitary json objects 
// or data you don't know it's tructure
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
----------------------------------------------

// Using Structs 
type response2 struct {
    Page   int      `json:"page"`
    Fruits []string `json:"fruits"`
}

res2D := &response2{
    Page:   1,
    Fruits: []string{"apple", "peach", "pear"}}
res2B, _ := json.Marshal(res2D)
fmt.Println(string(res2B))

--------------------------------------
// NewEncoder We can also stream JSON encodings directly to os.Writers like os.Stdout 
// or even HTTP response bodies.
d := map[string]int{"apple": 5, "lettuce": 7}
json.NewEncoder(os.Stdout).Encode(d)
```

### Decoding

```go
// Using Maps for aribitary json objects or data you don't know it's tructure
byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
    panic(err)
}
fmt.Println(dat)
-----------------------------

// Using Structs
type response2 struct {
    Page   int      `json:"page"`
    Fruits []string `json:"fruits"`
}

str := `{"page": 1, "fruits": ["apple", "peach"]}`
res := response2{}
json.Unmarshal([]byte(str), &res)
fmt.Println(res)
fmt.Println(res.Fruits[0])
--------------------------------------

// Using NewDecoder()
response := `{"page": 1, "fruits": ["apple", "peach"]}`
resStruct := response2{}
json.NewEncoder(response.Body).Encode(resStruct)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sec88.0x88.online/programming/go/package/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
