Làm cách nào để giải mã JSON trong Golang?

JSON [Ký hiệu đối tượng JavaScript] là định dạng văn bản trao đổi dữ liệu nhẹ. Nó có nghĩa là để con người đọc nhưng cũng dễ dàng đọc bằng máy và dựa trên một tập hợp con của JavaScript. JSON ban đầu được định nghĩa bởi Douglas Crockford, nhưng hiện được mô tả bởi RFC 7159, cũng như ECMA-404. JSON được sử dụng phổ biến trong các dịch vụ web dựa trên REST, mặc dù chúng không nhất thiết phải chấp nhận hoặc trả về dữ liệu JSON

Ảnh của Paul Esch-Laurent trên Bapt

JSON thực sự phổ biến với các dịch vụ web RESTful nhưng nó cũng thường được sử dụng để cấu hình. Việc tạo và sử dụng JSON là phổ biến trong nhiều ứng dụng web, từ việc lấy dữ liệu từ dịch vụ web, đến xác thực ứng dụng web của bạn thông qua dịch vụ xác thực của bên thứ ba, đến kiểm soát các dịch vụ khác

Go hỗ trợ JSON trong thư viện chuẩn bằng gói

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
9

sắp xếp lại JSON

Phân tích cú pháp JSON bằng

main.Person{
Name: “Luke Skywalker”,
Height: “172”,
Mass: “77”,
HairColor: “blond”,
SkinColor: “fair”,
EyeColor: “blue”,
BirthYear: “19BBY”,
Gender: “male”,
Homeworld: “//swapi.dev/api/planets/1/”,
Films: {"//swapi.dev/api/films/1/", “//swapi.dev/api/films/2/", “//swapi.dev/api/films/3/", “//swapi.dev/api/films/6/"},
Species: {},
Vehicles: {“//swapi.dev/api/vehicles/14/", “//swapi.dev/api/vehicles/30/"},
Starships: {“//swapi.dev/api/starships/12/", “//swapi.dev/api/starships/22/"},
Created: time.Date[2014, time.December, 9, 13, 50, 51, 644000000, time.UTC],
Edited: time.Date[2014, time.December, 20, 21, 17, 56, 891000000, time.UTC],
URL: “//swapi.dev/api/people/1/",
}
0 rất đơn giản

  1. Tạo cấu trúc để chứa dữ liệu JSON
  2. Sắp xếp lại chuỗi JSON thành các cấu trúc

Hãy lấy một tệp JSON mẫu, chứa dữ liệu về nhân vật Luke Skywalker trong Chiến tranh giữa các vì sao, được lấy từ SWAPI, API Chiến tranh giữa các vì sao. Bạn có thể truy cập trực tiếp tại đây — https. //hoán đổi. nhà phát triển/api/người/1. Tôi đã lấy dữ liệu và lưu trữ nó trong một tệp có tên là

main.Person{
Name: “Luke Skywalker”,
Height: “172”,
Mass: “77”,
HairColor: “blond”,
SkinColor: “fair”,
EyeColor: “blue”,
BirthYear: “19BBY”,
Gender: “male”,
Homeworld: “//swapi.dev/api/planets/1/”,
Films: {"//swapi.dev/api/films/1/", “//swapi.dev/api/films/2/", “//swapi.dev/api/films/3/", “//swapi.dev/api/films/6/"},
Species: {},
Vehicles: {“//swapi.dev/api/vehicles/14/", “//swapi.dev/api/vehicles/30/"},
Starships: {“//swapi.dev/api/starships/12/", “//swapi.dev/api/starships/22/"},
Created: time.Date[2014, time.December, 9, 13, 50, 51, 644000000, time.UTC],
Edited: time.Date[2014, time.December, 20, 21, 17, 56, 891000000, time.UTC],
URL: “//swapi.dev/api/people/1/",
}
1

Để lưu trữ dữ liệu trong JSON, chúng ta có thể tạo một cấu trúc như thế này

Như bạn có thể thấy từ cấu trúc, chúng ta có thể xác định các lát chuỗi để lưu trữ các mảng trong JSON và cũng sử dụng một cái gì đó như

main.Person{
Name: “Luke Skywalker”,
Height: “172”,
Mass: “77”,
HairColor: “blond”,
SkinColor: “fair”,
EyeColor: “blue”,
BirthYear: “19BBY”,
Gender: “male”,
Homeworld: “//swapi.dev/api/planets/1/”,
Films: {"//swapi.dev/api/films/1/", “//swapi.dev/api/films/2/", “//swapi.dev/api/films/3/", “//swapi.dev/api/films/6/"},
Species: {},
Vehicles: {“//swapi.dev/api/vehicles/14/", “//swapi.dev/api/vehicles/30/"},
Starships: {“//swapi.dev/api/starships/12/", “//swapi.dev/api/starships/22/"},
Created: time.Date[2014, time.December, 9, 13, 50, 51, 644000000, time.UTC],
Edited: time.Date[2014, time.December, 20, 21, 17, 56, 891000000, time.UTC],
URL: “//swapi.dev/api/people/1/",
}
2 làm kiểu dữ liệu. Trên thực tế, chúng tôi có thể sử dụng hầu hết các loại dữ liệu Go và thậm chí cả bản đồ [mặc dù chỉ hỗ trợ bản đồ có chuỗi làm khóa]

Sắp xếp lại dữ liệu vào cấu trúc chỉ là một lệnh gọi hàm duy nhất, sử dụng

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
0

Trong đoạn mã trên, sau khi đọc dữ liệu từ tệp, chúng tôi tạo một cấu trúc

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
1 và sau đó sắp xếp lại dữ liệu vào đó bằng cách sử dụng
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
0

Nhận trực tiếp từ API

Dữ liệu JSON đến từ API Chiến tranh giữa các vì sao, vì vậy hãy vui vẻ một chút và lấy dữ liệu trực tiếp từ API. Chúng tôi sử dụng hàm

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
3 và chuyển URL vào, nhưng mọi thứ khác đều giống nhau

Dòng cuối cùng trong đoạn mã trên in trực tiếp giá trị trong biểu diễn cú pháp Go của giá trị. Chúng tôi đang sử dụng trình định dạng đẹp ở đây để làm cho đầu ra đẹp hơn. Đây là những gì chúng ta nên nhận được

main.Person{
Name: “Luke Skywalker”,
Height: “172”,
Mass: “77”,
HairColor: “blond”,
SkinColor: “fair”,
EyeColor: “blue”,
BirthYear: “19BBY”,
Gender: “male”,
Homeworld: “//swapi.dev/api/planets/1/”,
Films: {"//swapi.dev/api/films/1/", “//swapi.dev/api/films/2/", “//swapi.dev/api/films/3/", “//swapi.dev/api/films/6/"},
Species: {},
Vehicles: {“//swapi.dev/api/vehicles/14/", “//swapi.dev/api/vehicles/30/"},
Starships: {“//swapi.dev/api/starships/12/", “//swapi.dev/api/starships/22/"},
Created: time.Date[2014, time.December, 9, 13, 50, 51, 644000000, time.UTC],
Edited: time.Date[2014, time.December, 20, 21, 17, 56, 891000000, time.UTC],
URL: “//swapi.dev/api/people/1/",
}
Dữ liệu phi cấu trúc

Cấu trúc của Star Wars API khá rõ ràng. Tuy nhiên điều này không phải lúc nào cũng đúng. Đôi khi chúng ta không biết cấu trúc đủ rõ để tạo cấu trúc và tài liệu không có sẵn. Ngoài ra, đôi khi các khóa cho các giá trị có thể động. Hãy xem tệp JSON này, được gọi là

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
4

Rõ ràng từ JSON, các khóa không nhất quán và có thể thay đổi khi thêm ký tự. Đối với những trường hợp như vậy, làm cách nào để sắp xếp lại dữ liệu JSON? . Hãy nhìn vào mã

Chúng ta hãy nhìn vào đầu ra

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}

Hãy thử mã tương tự trên dữ liệu JSON của Luke Skywalker trước đó và xem đầu ra. Chúng tôi sẽ chỉ sử dụng cùng một mã nhưng thay đổi tệp thành

main.Person{
Name: “Luke Skywalker”,
Height: “172”,
Mass: “77”,
HairColor: “blond”,
SkinColor: “fair”,
EyeColor: “blue”,
BirthYear: “19BBY”,
Gender: “male”,
Homeworld: “//swapi.dev/api/planets/1/”,
Films: {"//swapi.dev/api/films/1/", “//swapi.dev/api/films/2/", “//swapi.dev/api/films/3/", “//swapi.dev/api/films/6/"},
Species: {},
Vehicles: {“//swapi.dev/api/vehicles/14/", “//swapi.dev/api/vehicles/30/"},
Starships: {“//swapi.dev/api/starships/12/", “//swapi.dev/api/starships/22/"},
Created: time.Date[2014, time.December, 9, 13, 50, 51, 644000000, time.UTC],
Edited: time.Date[2014, time.December, 20, 21, 17, 56, 891000000, time.UTC],
URL: “//swapi.dev/api/people/1/",
}
1

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}

Bạn có thể nghĩ rằng điều này dễ dàng và đơn giản hơn nhiều so với việc cố gắng tìm ra các cấu trúc. Ngoài ra, nó dễ tha thứ và linh hoạt hơn rất nhiều, tại sao không sử dụng cái này thay thế? . Sử dụng các giao diện trống về cơ bản làm cho cấu trúc dữ liệu không được nhập. Các cấu trúc có thể bắt lỗi trong JSON trong đó các giao diện trống chỉ cần bỏ qua. Việc truy xuất dữ liệu từ các cấu trúc cũng dễ dàng hơn nhiều so với từ bản đồ vì bạn biết chắc chắn những trường nào có sẵn. Thông thường, bạn nên cố gắng sử dụng các cấu trúc và chỉ ánh xạ tới các giao diện trống như là phương sách cuối cùng

Giải mã JSON

Sử dụng

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6 rất đơn giản và dễ hiểu đối với các tệp JSON hoặc dữ liệu API. Nhưng điều gì sẽ xảy ra nếu API đang truyền dữ liệu JSON? . Thay vào đó, gói
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
9 cung cấp một
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
0 để chúng tôi xử lý dữ liệu

Có thể khó hiểu sự khác biệt giữa dữ liệu JSON và truyền dữ liệu JSON. Hãy so sánh 2 tệp JSON, đại diện cho dữ liệu JSON bình thường và dữ liệu JSON truyền phát

Trong tệp JSON đầu tiên này, chúng tôi có một mảng gồm 3 đối tượng JSON [tôi đã cắt bớt một phần dữ liệu để dễ đọc hơn]

Để đọc phần này, chúng ta có thể sử dụng

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6 bằng cách sắp xếp lại thành một mảng gồm các cấu trúc
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
1. Điều này rất giống với những gì chúng ta đã thấy trước đó

Điều này sẽ dẫn đến một bản in ra bàn điều khiển trông như thế này

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
4

Đây là một mảng các cấu trúc Person mà chúng tôi nhận được sau khi sắp xếp lại một mảng JSON

Tuy nhiên, khi chúng tôi nhận được một luồng các đối tượng JSON, điều này không còn khả thi nữa. Hãy xem xét một tệp JSON khác, một tệp đại diện cho luồng dữ liệu JSON

Lưu ý rằng đây không phải là một đối tượng JSON đơn lẻ mà là 3 đối tượng JSON liên tiếp. Trên thực tế, đây không phải là tệp JSON hợp lệ nhưng nó là thứ bạn có thể nhận được khi đọc

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
3 của cấu trúc
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
4. Nếu bạn cố đọc phần này bằng cách sử dụng
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6, bạn sẽ nhận được thông báo lỗi

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
8

Tuy nhiên, bạn có thể phân tích nó bằng cách giải mã nó bằng cách sử dụng

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
0

Đầu tiên, chúng tôi tạo bộ giải mã bằng cách sử dụng

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
7 và chuyển nó cho người đọc. Trong trường hợp này, đó là tệp chúng tôi đọc từ. Sau đó, trong khi chúng tôi đang lặp trong vòng lặp
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
8, chúng tôi gọi
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9 trên bộ giải mã, chuyển cho nó cấu trúc mà chúng tôi muốn lưu trữ dữ liệu trong đó. Nếu mọi việc suôn sẻ, mỗi khi nó lặp lại, một cấu trúc
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
1 mới được tạo từ dữ liệu. Sau đó, chúng ta có thể sử dụng dữ liệu hoặc chúng ta cũng có thể lưu trữ dữ liệu đó trong một mảng. Nếu không còn dữ liệu trong đầu đọc, tôi. e. chúng tôi nhấn
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
41, chúng tôi sẽ
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
42 từ vòng lặp
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
8

Đây là đầu ra từ đoạn mã trên

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
7

Bạn có thể thấy rằng có 3 cấu trúc

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
1 được in ra ở đây, nối tiếp nhau, trái ngược với cấu trúc trước đó là một mảng gồm các cấu trúc
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
1

Một câu hỏi đôi khi được đặt ra là khi nào chúng ta nên sử dụng

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6 và khi nào chúng ta nên sử dụng
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9?

map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6 dễ sử dụng hơn cho một đối tượng JSON nhưng nó sẽ không hoạt động khi bạn có một luồng chúng đến từ một trình đọc. Ngoài ra, tính đơn giản của nó có nghĩa là nó không linh hoạt, bạn chỉ cần lấy toàn bộ dữ liệu JSON một lần

Mặt khác,

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9 hoạt động tốt cho cả đối tượng JSON đơn lẻ hoặc truyền dữ liệu JSON. Ngoài ra, với
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9, bạn có thể thực hiện mọi thứ với JSON ở cấp độ tốt hơn mà không cần phải lấy toàn bộ dữ liệu JSON ra trước. Điều này là do bạn có thể kiểm tra JSON khi nó xuất hiện, ngay cả ở cấp độ mã thông báo. Tuy nhiên, nhược điểm nhỏ là nó dài dòng hơn

map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9 cũng hoạt động tốt hơn
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6, vì vậy nếu bạn muốn phân tích cú pháp nhanh hơn, bạn nên sử dụng
map[string]interface {}{
“birth_year”: “19BBY”,
“created”: “2014–12–09T13:50:51.644000Z”,
“edited”: “2014–12–20T21:17:56.891000Z”,
“eye_color”: “blue”,
“films”: []interface {}{
“//swapi.dev/api/films/1/”,
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“gender”: “male”,
“hair_color”: “blond”,
“height”: “172”,
“homeworld”: “//swapi.dev/api/planets/1/",
“mass”: “77”,
“name”: “Luke Skywalker”,
“skin_color”: “fair”,
“species”: []interface {}{
},
“starships”: []interface {}{
“//swapi.dev/api/starships/12/”,
“//swapi.dev/api/starships/22/",
},
“url”: “//swapi.dev/api/people/1/",
“vehicles”: []interface {}{
“//swapi.dev/api/vehicles/14/",
“//swapi.dev/api/vehicles/30/",
},
}
9 thay vì
map[string]interface {}{
“C-3P0”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
“Darth Vader”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“Luke Skywalker”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/6/",
},
“R2D2”: []interface {}{
“//swapi.dev/api/films/1/",
“//swapi.dev/api/films/2/",
“//swapi.dev/api/films/3/",
“//swapi.dev/api/films/4/",
“//swapi.dev/api/films/5/",
“//swapi.dev/api/films/6/",
},
}
6, đặc biệt khi bạn có tệp lớn hơn hoặc có lượng dữ liệu lớn

Làm cách nào để đọc tệp JSON trong Golang?

json được đọc bằng ioutil. Hàm ReadFile[] , trả về một lát byte được giải mã thành thể hiện cấu trúc bằng cách sử dụng json. Hàm không nguyên soái []. Cuối cùng, các giá trị thành viên của cá thể cấu trúc được in bằng vòng lặp for để chứng minh rằng tệp JSON đã được giải mã.

Mã hóa và giải mã JSON trong Golang là gì?

JSON là định dạng được sử dụng rộng rãi để trao đổi dữ liệu. Golang cung cấp nhiều API mã hóa và giải mã để hoạt động với JSON bao gồm đến và từ các loại dữ liệu tùy chỉnh và tích hợp bằng cách sử dụng gói mã hóa/json . Loại dữ liệu. Các kiểu dữ liệu Golang mặc định để giải mã và mã hóa JSON như sau. bool cho booleans JSON.

Làm cách nào để phân tích dữ liệu trong Golang?

Phân tích cú pháp dữ liệu JSON bằng Golang .
Bước 1. Tạo tệp dữ liệu JSON. Đầu tiên chúng ta sẽ tạo một tệp JSON worker đơn giản. .
Bước 2. Đọc tệp dữ liệu JSON. Để mở tệp JSON, chúng tôi sẽ sử dụng gói os. .
Bước 3. Phân tích dữ liệu JSON bằng cấu trúc. .
Bước 4. Sắp xếp lại dữ liệu JSON. .
Bước5. Mã hoàn chỉnh để phân tích dữ liệu JSON bằng Go

JSON marshalling trong Golang là gì?

Sắp xếp. Chuyển đổi các đối tượng Go thành JSON . Chúng ta có thể sử dụng hàm Marshal để chuyển các đối tượng Go thành JSON. Hàm Marshal đi kèm với cú pháp sau. func Marshal[v interface{}] [[]byte, error] Nó chấp nhận một giao diện trống. Encoding Go objects to JSON format is known as marshaling. We can use the Marshal function to convert Go objects to JSON. The Marshal function comes with the following syntax. func Marshal[v interface{}] [[]byte, error] It accepts an empty interface.

Chủ Đề