Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

json - How to concat two JSONObject with same key of a JSONArray in flutter

The JSON request has been split into two json object in the DataList JSONArray , because data is too large, how do i combine these two objects before i can decompress and get the values . Iam new to dart and flutter , any help would be appreciated. Thank you.

 "DataList": [
                    {
                        "Data": "compressedata"
                    },
                    {
                        "Data": "compressedData"
                    }
                ],

here is what i have tried

class ResponseList {
 
 List<DataList> dataList;
 
 ResponseList({ this.DataList});

 ResponseList.fromJson(Map<String, dynamic> json) {
  if (json['DataList'] != null) {
   DataList = new List<DataList>();
    json['DataList'].forEach((v) {
    dataList.add(new DataList.fromJson(v));
    });
  }
  
 }

 Map<String, dynamic> toJson() {
    final Map<String, dynamic> map = new Map<String, dynamic>();
    if (this.DataList != null) {
    map['DataList'] = this.dataList.map((v) => v.toJson()).toList();
  }
  
  return map;
 }
}
class DataList {
 String data;
 DataList({this.data});
 DataList.fromJson(Map<String, dynamic> json) {
  data = json['Data'];

 }

 Map<String, dynamic> toJson() {
  final Map<String, dynamic> map = new Map<String, dynamic>();
  map['Data'] = this.data;
  return map;
 }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using the Datalist array you can do the following:

 var dataList = [
    {"Data": "compressedata"},
    {"Data": "compressedData"}
  ];

  var compressedData = dataList
      .map((item) => item["Data"])
      .reduce((value, element) => value + element);

  print(compressedData); // compressedatacompressedData

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...