json - 基于条件的不同结果的 JSON 响应 - 需要指南
问题描述
在 JSON 中包含或排除其值取决于另一个字段的字段的最佳方法是什么。在下面的示例中,phone_number 和 email 字段将具有基于 auth_type 字段的值或 null。
在方法 A 中,如果 auth_type 是 phone 则 email 字段不会在 api 响应中发送,但在方法 B 中,如果 auth_type 是 phone 那么 email 字段将发送空值。哪种方法好?
方法一:
{
id: 123,
...
auth_type: 'Phone',
phone_number: 123456
}
{
id: 123,
...
auth_type: 'email',
email: 'abc@abc.com'
}
方法 B:
{
id: 123,
...
auth_type: 'Phone',
phone_number: 123456,
email: null
}
{
id: 123,
...
auth_type: 'email',
phone_number: null,
email: 'abc@abc.com'
}
解决方案
推荐阅读
- symfony - Shopware (5):哪个 Shopware 版本使用哪个最低 PHP 版本?
- python - Kivy 按钮文本对齐 (halign) 不起作用
- azure - 如何区分 IoT Central 中来自多个类似传感器(Philips Hue 灯泡)的数据到连接到 IoT Central 的网关设备
- android - 从linearLayout中动态生成的多个微调器获取数据
- objective-c - 使用基于变量的属性扩展objective-c中的类
- css - 滑块 ui 在基于铬的浏览器中中断
- java - 无法在java的子模块中使用父模块类
- java - 如何使用杰克逊为地图创建虚拟属性
- internationalization - 如何使用 gatsby-source-strapi-localized
- excel - Excel XML:微软命名空间的 XLST 转换问题?