How to encode a json table?
there is

type User struct { Id int FIO string Login string Password string ..... } var UsersList = make(map[int] User ) 

I fill in the UsersList. It is necessary to sort out, for example, where Id = 1,2,3. How can I get this list to json? looked Marschal, but I do not know where to cling it.

    1 answer 1

    Still, read in more detail something on the language, the same serialization in JSON where it is not described.

    Here is an example of serializing what you need:

     package main import ( "fmt" "strconv" "encoding/json" ) type User struct { Id int FIO string Login string Password string } func main() { // Тестовые данные userList := make(map[int]User) userList[1] = User{Id: 1, FIO: "111", Login: "111", Password: "111"} userList[2] = User{Id: 2, FIO: "222", Login: "222", Password: "222"} userList[3] = User{Id: 3, FIO: "333", Login: "333", Password: "333"} userList[4] = User{Id: 4, FIO: "444", Login: "444", Password: "444"} userList[5] = User{Id: 5, FIO: "555", Login: "555", Password: "555"} // Идентификаторы пользователей, которых нужно сериализовать. idsToEncode := []int{1, 2, 3} // Собираем данные для сериализации. // (в JSON нельзя использовать целые числа в качестве ключей) mapToEncode := make(map[string]User) for _, id := range idsToEncode { mapToEncode[strconv.Itoa(id)] = userList[id] } // Сериализуем данные. data, err := json.Marshal(mapToEncode) if err != nil { fmt.Printf("error: %v\n", err) return } // Выводим полученный JSON. fmt.Printf("data: %v\n", string(data)) } 

    Update . Well, this is a simple loop with the condition, if you need to filter something else in a different way, it's the basics!

      ... // Собираем данные для сериализации. mapToEncode := make(map[string]User) for id, user := range userList { if user.FIO == "111" { mapToEncode[strconv.Itoa(id)] = user } } ... 
    • Thank you so much)) - Rakzin Roman
    • And another question. If a little bit differently: Is there still a parent field, and you need to display, for example, everyone who has ParentId == 111? - Rakzin Roman
    • one
      @TwoRS, answered. - Vadim Shender