如何在Dart中将Map <string,object =“”>转换为Map <key,value =“”>?

时间:2019-05-17 06:10:59

标签: dart flutter dart-async

我有这个String Map,Object,我希望将其展平,因此结果是Key,Value Map

void main(){


    var li = {"qotd_date":"2019-05-18T00:00:00.000+00:00","quote":{"id":61869,"dialogue":false,"private":false,"tags":[],"url":"https://favqs.com/quotes/wael-ghonim/61869-the-power-of--","favorites_count":1,"upvotes_count":0,"downvotes_count":0,"author":" Wael Ghonim ","author_permalink":"wael-ghonim","body":"The power of the people is greater than the people in power."}};


    print(li);

}

当前输出:

{qotd_date: 2019-05-18T00:00:00.000+00:00, quote: {id: 61869, dialogue: false, private: false, tags: [], url: https://favqs.com/quotes/wael-ghonim/61869-the-power-of--, favorites_count: 1, upvotes_count: 0, downvotes_count: 0, author:  Wael Ghonim , author_permalink: wael-ghonim, body: The power of the people is greater than the people in power.}}

我想要的预期输出是

{"qotd_date":"2019-05-18T00:00:00.000+00:00","id":61869,"dialogue":false,"private":false,"tags":[],"url":"https://favqs.com/quotes/wael-ghonim/61869-the-power-of--","favorites_count":1,"upvotes_count":0,"downvotes_count":0,"author":" Wael Ghonim ","author_permalink":"wael-ghonim","body":"The power of the people is greater than the people in power."};

就像平展地图一样。请帮助我摆脱困境吗?

1 个答案:

答案 0 :(得分:1)

简单地说,编码为json:

#OPEN CSV
$filevalue = 'C:\test\20180720-Name-Location-0001.csv'
$path = 'C:\test'
$file = 'test.txt'
$file2 = '20180720-Name-Location-0001.csv'
$date = Get-Date -Format "yyyy-MM-dd-"
$filename = ($date + "-" + "ACME-YELP-36-AL_01")

Copy-Item "C:\test\20180720-Name-Location-0001.csv" -Destination "C:\test\$filename.txt"


#$originalFiles = Get-ChildItem "C:test\" -Filter *.txt
#$x = ("-000" + 1 + ".txt")

#Gets Todays Date

$Today = Get-Date -Format 'yyyy-MM-dd'

# delete any existing test file
#$Null = Remove-Item -Path "$env:TEMP\$($Today)_*.txt" -ErrorAction SilentlyContinue
# create a file to work with
#$Null = New-Item -Path $env:TEMP -Name "$($Today)_07.txt" -ItemType File -ErrorAction SilentlyContinue

# pretend to access the listing 17 times
1..17 |
    ForEach-Object {
        $FileInfo = Get-ChildItem -LiteralPath "C:\test\" -Filter "*.txt"
        $OldSeqNumber = $FileInfo.BaseName.Split('_')[-1]
        $NewSeqNumber = '{0:D2}' -f ([int]$OldSeqNumber + 1)

        $NewFileName = $FileInfo.Name.Replace("_$OldSeqNumber", "_$NewSeqNumber")

        Rename-Item -LiteralPath "C:\test\2019-05-08--Name-LOC_01.txt" $FileInfo.FullName -NewName $NewFileName
        }


输出:

import 'dart:convert';

void main() {
  Map<String, dynamic> li = {
    "qotd_date": "2019-05-18T00:00:00.000+00:00",
    "quote": {
      "id": 61869,
      "dialogue": false,
      "private": false,
      "tags": [],
      "url": "https://favqs.com/quotes/wael-ghonim/61869-the-power-of--",
      "favorites_count": 1,
      "upvotes_count": 0,
      "downvotes_count": 0,
      "author": " Wael Ghonim ",
      "author_permalink": "wael-ghonim",
      "body": "The power of the people is greater than the people in power."
    }
  };

  Map<String, dynamic> flattened = {};

  li.removeWhere((key, value) {
    if (value is Map) {
      flattened.addAll(value);
    }
    return value is Map;
  });

  flattened.addAll(li);

  print(jsonEncode(flattened));
}


相关问题