【问题标题】:how to convert Json value to List<String> in flutter如何在颤动中将 Json 值转换为 List<String>
【发布时间】:2020-11-25 00:07:27
【问题描述】:

这是我的 API 响应,我希望将所有建筑物名称添加到字符串列表中

{
    "entity_id": "86",
    "building_name": "Burj Khalifa",
    "location": "Al  Ttay",
    "image_field": "1595916594oad.jpeg"
},
{
    "entity_id": "87",
    "building_name": "Azmair",
    "location": " Eyal Nasser ",
    "image_field": "1596541099s.jpeg"
},

]

我试过了,但没用

List<String> _buildlist = (jsonDecode(response['building_name']) as List<dynamic>).cast<String>();
print(_buildlist);

【问题讨论】:

    标签: android ios flutter dart flutter-layout


    【解决方案1】:

    你需要对JSON进行解码,然后将map每个元素转换成相关的字符串,重新组成一个列表:

      var decoded = json.decode(j) as List;
      var names = decoded.map<String>((e) => e['building_name']).toList();
      print(names); // prints [Burj Khalifa, Azmair]
    

    【讨论】:

      【解决方案2】:

      使用包 'dart:convert' 尝试以下操作:

      import 'dart:convert';
      
      void main() {
        
        var json = '''[
          {
            "id":"67890",
            "name":"Hello World",
            "score" : 1000
          },
          {
            "id":"12345",
            "name":"Hello Sky",
            "score" : 2000
          }
        ]''';
        
        print('json is $json');
        var users = jsonDecode(json);
        assert(users is List);
        print('users is $users');
      }
      

      你可以运行https://dartpad.dev/中的代码并测试一下

      【讨论】:

        【解决方案3】:

        首先,您需要在数组列表中获取所有字段的值。首先,您需要解析所有响应。使用app.quicktype.io 为任何响应创建适当的模型类。这是来自 app.quicktype.io 的模型类,

        import 'dart:convert';
        
        List<ParseResponse> parseResponseFromJson(String str) => List<ParseResponse>.from(json.decode(str).map((x) => ParseResponse.fromJson(x)));
        
        String parseResponseToJson(List<ParseResponse> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
        
        class ParseResponse {
            ParseResponse({
                this.entityId,
                this.buildingName,
                this.location,
                this.imageField,
            });
        
            String entityId;
            String buildingName;
            String location;
            String imageField;
        
            factory ParseResponse.fromJson(Map<String, dynamic> json) => ParseResponse(
                entityId: json["entity_id"],
                buildingName: json["building_name"],
                location: json["location"],
                imageField: json["image_field"],
            );
        
            Map<String, dynamic> toJson() => {
                "entity_id": entityId,
                "building_name": buildingName,
                "location": location,
                "image_field": imageField,
            };
        }
        

        现在如果你调用 parseResponseFromJson(resonseString);然后你就会有列表,你可以从这里开始。

        【讨论】:

          【解决方案4】:

          您需要首先使用 json.decode() 将 JSON 转换为地图列表。然后迭代地图列表以创建新的建筑物名称列表。请记住显式声明尽可能多的变量并减少使用“var”关键字。它有助于编译器更快地编译程序并增加代码的可读性。

          下面的代码展示了我的方法:

          import 'dart:convert';
          void main() {
            
           var response =[
              {
              "entity_id": "86",
              "building_name": "Burj Khalifa",
              "location": "Al  Ttay",
              "image_field": "1595916594oad.jpeg"
              },
              {
              "entity_id": "87",
              "building_name": "Azmair",
              "location": " Eyal Nasser ",
              "image_field": "1596541099s.jpeg"
              },
           ];
           List<String> _buildlist = List();
           List<Map<String,String>> data = json.decode(response);
           for(Map<String,String> i in data)
              _buildlist.add(i["building_name"]);
          
           print(_buildlist);
          }
          

          【讨论】:

            猜你喜欢
            • 2020-09-15
            • 2021-09-14
            • 2022-08-18
            • 2021-09-26
            • 1970-01-01
            • 2019-07-30
            • 1970-01-01
            • 2020-04-16
            • 2021-04-28
            相关资源
            最近更新 更多