将JSON数据更改为颤振数组组

时间:2019-06-13 02:53:24

标签: collections flutter

我有一些数据JSON,如下所示:

[     {         “ KD”:“ 1”,         “ NAME”:“ Name 1”,         “ JENIS”:“组1”     },     {         “ KD”:“ 2”,         “ NAME”:“ Name 2”,         “ JENIS”:“组1”     },     {         “ KD”:“ 3”,         “ NAME”:“ Name 3”,         “ JENIS”:“ GROUP 2”     },     {         “ KD”:“ 4”,         “ NAME”:“ Name 4”,         “ JENIS”:“ GROUP 2”     } ]

如何变成这样的数组组 我的预期结果是:

array (size=2)
  'GROUP 1' => 
    array (size=2)
      0 => 
        array (size=3)
          'KD' => string '1' (length=1)
          'NAME' => string 'Name 1' (length=6)
          'JENIS' => string 'GROUP 1' (length=7)
      1 => 
        array (size=3)
          'KD' => string '2' (length=1)
          'NAME' => string 'Name 2' (length=6)
          'JENIS' => string 'GROUP 1' (length=7)
  'GROUP 2' => 
    array (size=2)
      0 => 
        array (size=3)
          'KD' => string '3' (length=1)
          'NAME' => string 'Name 3' (length=6)
          'JENIS' => string 'GROUP 2' (length=7)
      1 => 
        array (size=3)
          'KD' => string '4' (length=1)
          'NAME' => string 'Name 4' (length=6)
          'JENIS' => string 'GROUP 2' (length=7)

我已经尝试过类似的方法,但这不值得:

if (response.statusCode == 201) {
      var jsonResponse = json.decode(response.body);
      List data = jsonResponse['data']['hasil']['result'];
      setState(() {
        Map<String, dynamic> dataJSON = Map<String, dynamic>();
        for (var x = 0; x < data.length; x++) {
          dataJSON[data[x]["JENIS"]][]=data[x];
        }
        print(dataJSON);
      });
    }

但这不值得。

0 个答案:

没有答案