json_serializable flutter parse并与钥匙连接与模态和映射器的映射
Thais是我的JSON格式,
"recommended": {
"section_title": "Recommended",
"section_sub_title": "Recommended",
"data": {
"Allahabad": [
{
"gym_id": "9",
"name": "raga fitness",
"address": "35 Vivekanand Marg Hewett Road Allahabad",
"city_id": "1565",
"gym_logo": "http://139.180.218.66/gym/resources/alias_raga_fitness_20220403165957.jpeg",
"no_of_ratings": null,
"total_ratings": null,
"average_ratings": null,
"city_name": "Allahabad"
}
],
"Lucknow": [
{
"gym_id": "2",
"name": "Gym Name 2",
"address": "gym address",
"city_id": "1496",
"gym_logo": "http://139.180.218.66/gym/resources/alias_Gym_Name_20220127182703.jpeg",
"no_of_ratings": "16",
"total_ratings": "55.5",
"average_ratings": "3.5",
"city_name": "Lucknow"
}
]
}
}
我使用JSON_SERIALIZABLE,DIO和JSON_ANNOTATION,希望用模态和响应信息映射
我正在使用json_serializable在Flutter中使用JSON_Serializable将类存储在文件中并从中读回。我不是为了简单起见,在这里发布原始类,但是原则是编写应用程序的一半,我决定要将可变名称“ at astupidname”更改为“名称”。我如何建议代码生成实用程序将JSON值分配给密钥“ AstupidName”,如果它存在于JSON中,则将其分配给变量“名称”,但是如果存在键“名称”以将其分配给变量,则IE在文件的较新版本中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案1
在模型类中,假设
xyz
,使用@jsonkey
映射一个从JSON读取字段的方式。这可以用 /a>进行2个参数:json数据作为map< string,动态>
和字段名称。这将是以下内容。另外,。我还建议将API/DB/正常化为标准化,以始终为
name
,因此无需以这种方式注释。解决方案2
另一方面, 可以像以下内容一样通过手工实现序列化/避难所化。只需使用操作员条件表达式
名称
和astupidname
之间。Solution 1
In the model class, let's say
XYZ
, use the@JsonKey
to map a how a field is read from JSON. This can be done withJsonKey.readValue
that takes 2 arguments: the JSON data asMap<String, dynamic>
and the field name. It's going to be something like the following. Also, check out the official docs.I'd also recommend normalizing the API/DB/whatever itself to be always
name
so there is no need to annotate this way.Solution 2
On the other hand, it's possible to implement the serialization/deserialization by hand like the following. Just use the ?? operator conditional expression between
name
andaStupidName
.