json - 定义结构并将其编组为 json 的问题
问题描述
在 struct / json 字符串中表示以下内容的最佳方式是什么?-如果可能的话-
我想处理的数据看起来像:
{{Database : "flowers" , Type : "sunflower" - Location : "behind"
- Height : "130"
, Type : "roses" - Time : "12:30"
- Date : "12-12-2019"
- Height : "150" },
{
Database : "fruits" , Type : "apple" - Height : "200"
, Type : "peer" - Location : "above"
, Type : "banana" - Color : "green" }}
- 特定数据库中的某些项目,例如“向日葵”和“苹果”,其规格比“玫瑰”少或不同
任何从哪里开始的提示或任何想法都将非常有帮助,非常感谢。
解决方案
There are many ways that you could represent this data in your code and in json, here is just a couple of ways you could section it dependent on what is important to you and what you would do with the data after it is in the structs/json.
type (
Plant struct {
Type string `json:"type"` //flowers or fruits
Attributes *PlantAttributes `json:"attributes"`
}
PlantAttributes struct {
Name string `json:"name"` //apple or roses etc
Location string `json:"location"`
Height string `json:"height"`
Time string `json:"time"`
Date string `json:"date"`
Color string `json:"color"`
}
)
or
type (
Flowers struct {
Type string `json:"type"` // apples or bananas
Attributes *PlantAttributes `json:"attributes"`
}
Fruit struct {
Type string `json:"type"` // sunflowers or roses
Attributes *PlantAttributes `json:"attributes"`
}
PlantAttributes struct {
Location string `json:"location"`
Height string `json:"height"`
Time string `json:"time"`
Date string `json:"date"`
Color string `json:"color"`
}
)
推荐阅读
- vb.net - 如何在 VB.NET 中使用 LINQ 从 XML 中检索数据?
- javascript - 循环 HTML 中的等待时间
- ajax - Laravel 删除上一个会话
- nancy - Nancy 不在视图上呈现模型数据 (VB.NET)
- python - Python:从一个数据框中获取值并添加到另一个(行和列条件)
- jquery - jquery autocomplete json 不会显示 - css 问题?
- java - 尝试在 Android 应用程序中实施 AdMob 时出现“无效的应用程序 ID”错误
- java - UCanAccess 似乎无法使用 getBytes() 读取 OLE 对象列
- retrofit - Retrofit2 不消耗端点 - Spotify API
- ajax - 为什么我在播放列表 spotify 上收到 401 错误?