Home  >  Article  >  Backend Development  >  golang json to object

golang json to object

WBOY
WBOYOriginal
2023-05-13 10:56:371139browse

In the Golang programming language, JSON is a very common data structure. Golang provides some easy-to-use APIs for us to perform JSON encoding and decoding operations. This article will introduce how to convert JSON to Golang objects. We'll illustrate how this is done with a concrete example.

First, we need to use the encoding/json package in Golang. This package provides methods to convert between Golang objects and JSON. The most important of them are Marshal and Unmarshal methods. Marshal is used to encode Golang objects into JSON strings, while Unmarshal is used to decode Golang objects from JSON strings.

Let's look at a specific example. Consider the following JSON string, which represents a user object stored in JSON.

{
    "id": 123,
    "name": "John Doe",
    "email": "jdoe@example.com",
    "phone": "555-1234",
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
    }
}

We will use Golang to write code to read this JSON and convert it into a User object. Our User object is defined as follows:

type User struct {
    ID      int
    Name    string
    Email   string
    Phone   string
    Address Address
}

Address is also an object type, which is used to represent the user's residential address:

type Address struct {
    Street string
    City   string
    State  string
    Zip    string
}

Let's see how to convert the above JSON into our User object. We can first use the Unmarshal method to read the JSON string and then convert it into a Golang structure:

func decodeUser(jsonStr string) (User, error) {
    var user User
    err := json.Unmarshal([]byte(jsonStr), &user)
    if err != nil {
        return User{}, err
    }
    return user, nil
}

We can pass the JSON string as a parameter to our decodeUser function through the following method to get a User object:

jsonStr := `{
    "id": 123,
    "name": "John Doe",
    "email": "jdoe@example.com",
    "phone": "555-1234",
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
    }
}`

user, err := decodeUser(jsonStr)
if err != nil {
    fmt.Println("Error decoding JSON: ", err.Error())
}
fmt.Printf("User: %+v", user)

The above code should output the following:

User: {ID:123 Name:John Doe Email:jdoe@example.com Phone:555-1234 Address:{Street:123 Main St City:Anytown State:CA Zip:12345}}

In Golang, using JSON is very convenient and easy to use. We only need to be familiar with some APIs in the json package to implement JSON encoding and decoding. Through the above introduction, readers can understand how Golang processes JSON and how to convert JSON strings into Golang objects.

The above is the detailed content of golang json to object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:golang int default numberNext article:golang int default number