有没有任何方法可以在dart中使用JsonSerializable创建JsonKey动态?我们可以从JsonSerial



主要问题:如何使用json_serializable解析这个json数组

[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "http://placehold.it/600/771796",
"thumbnailUrl": "http://placehold.it/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "http://placehold.it/600/24f355",
"thumbnailUrl": "http://placehold.it/150/24f355"
}
]

使用StringList嵌入自定义对象的简单示例:

import 'package:json_annotation/json_annotation.dart';
part 'all_json.g.dart';
@JsonSerializable()
class AllJsonData {
@JsonKey(name: 'some_key') // @Question: Can we make this some_key dynamic
List<String> orders;
AllJsonData(this.orders);
factory AllJsonData.fromJson(Map<String, dynamic> json) =>
_$AllJsonDataFromJson(json);
Map<String, dynamic> toJson() => _$AllJsonDataToJson(this);
}

all_json.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'all_json.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
AllJsonData _$AllJsonDataFromJson(Map<String, dynamic> json) {
return AllJsonData(
(json['some_key'] as List)?.map((e) => e as String)?.toList(),
);
}
Map<String, dynamic> _$AllJsonDataToJson(AllJsonData instance) =>
<String, dynamic>{
'some_key': instance.orders,
};

@问题1:我们能让这个some_key动态吗在生成对象并反序列化之后,它会给出这样的结果。

AllJsonData allJsonData = AllJsonData(['Some', 'People']);
String vv = jsonEncode(allJsonData.toJson());
print(vv);
// Getting output like:
"{"education":["Some","People"]}"
// Required Output only array without key:
["Some","People"]

@问题2:如何获得无键数组的输出:["Some","People"]

这是最简单的方法!

final list = (jsonDecode(text) as List).map((e) => Album.fromJson(e)).toList()

CCD_ 1假定所有对象都是CCD_。如果你想在最高级别处理一个列表,请按照我在这里所做的操作。

使用简单的String[]将模型从JsonArray转换为String。

AllJsonData allJsonData = AllJsonData(
orders: ['James', 'Bipin', 'Simon'],
);
String jsonObjectAsString = jsonEncode(allJsonData.toJson());
var parentMap = jsonDecode(jsonObjectAsString);
// @read: To convert as JsonString
var jsonString = jsonEncode(parentMap['orders'],
toEncodable: (e) => e.toString());
print(jsonObjectAsString); 
// gives result like "["James","Bipin","Simon"]"

对于自定义型号类别:

AllJsonData allJsonData = AllJsonData(
education: edu,
);
String jsonObjectAsString = jsonEncode(allJsonData.toJson());
var parentMap = jsonDecode(jsonObjectAsString);
var subParentMap = parentMap['education'];
List<dynamic> mList = subParentMap['modelList'];
// mapping as model-list
var modelList =
mList.map((e) => ExpPopupModel.fromJson(e)).toList();
// convert into json-string
var jsonString = jsonEncode(subParentMap['modelList'],
toEncodable: (e) => e.toString());
print(jsonObjectAsString);

相关内容

  • 没有找到相关文章

最新更新