将 ison 转换为可读的飞镖

Posted

技术标签:

【中文标题】将 ison 转换为可读的飞镖【英文标题】:convert ison to readable dart 【发布时间】:2021-01-11 06:53:24 【问题描述】:

我是 Flutter 框架的新手,我正在尝试从 REST API 获取数据到我的应用程序并对其进行可视化。我收到了 Jason 响应,我需要访问该 json 中的每个数据, 你能帮我把这个 json 转换成可读的 dart 格式,这样我就可以访问每个数据了吗? 如何在颤振飞镖中处理这种转换? 即使我至少可以在调试打印中看到它们也会很好。

非常感谢您对任何解释性贡献,

这是json

  "success": true,
  "pet": 
    "id": 17,
    "activity_level_name": "Low Activity (<1hr/day)",
    "daily_allowance_guidelines_details": [
      
        "name": "Muscle Meat",
        "bones": true,
        "percentage": 60
      ,
      
        "name": "Organ Meat",
        "bones": false,
        "percentage": 10
      ,
      
        "name": "Fruit and Veg",
        "bones": false,
        "percentage": 20
      ,
      
        "name": "Bone",
        "bones": true,
        "percentage": 10
      
    ],
    "eatbone": true,
    "ideal_weight": 3.3,
    "image": 
      "url": null,
      "thumb": 
        "url": null
      
    ,
    "name": "Roxy",
    "nutrient_guideline_detail": [
      
        "name": "Protein",
        "amount": 9.99,
        "unit": "g"
      ,
      
        "name": "Crude fat",
        "amount": 3.06,
        "unit": "g"
      ,
      
        "name": "Calcium/Phosphorus ratio",
        "amount": 0.44,
        "unit": "ratio"
      ,
      
        "name": "Omega-3/6 ratio",
        "amount": 0.06,
        "unit": "ratio"
      ,
      
        "name": "Omega-6",
        "amount": 0.62,
        "unit": "g"
      ,
      
        "name": "Omega-3 excl. ALA and SDA",
        "amount": 0.02,
        "unit": "g"
      ,
      
        "name": "Calcium",
        "amount": 0.28,
        "unit": "g"
      ,
      
        "name": "Phosphorus",
        "amount": 0.22,
        "unit": "g"
      ,
      
        "name": "Potassium",
        "amount": 0.33,
        "unit": "g"
      ,
      
        "name": "Sodium (Na)",
        "amount": 0.04,
        "unit": "mg"
      ,
      
        "name": "Magnesium",
        "amount": 0.03,
        "unit": "mg"
      ,
      
        "name": "Iron",
        "amount": 2.22,
        "unit": "mg"
      ,
      
        "name": "Copper",
        "amount": 0.41,
        "unit": "mg"
      ,
      
        "name": "Manganese",
        "amount": 0.28,
        "unit": "mg"
      ,
      
        "name": "Zinc (Zn)",
        "amount": 4.44,
        "unit": "mg"
      ,
      
        "name": "Iodine",
        "amount": 55.49,
        "unit": "mcg"
      ,
      
        "name": "Selenium",
        "amount": 0.02,
        "unit": "mcg"
      ,
      
        "name": "Vitamin A",
        "amount": 83.23,
        "unit": "mcg"
      ,
      
        "name": "Vitamin D",
        "amount": 0.69,
        "unit": "mcg"
      ,
      
        "name": "Vitamin E",
        "amount": 2.5,
        "unit": "mg"
      ,
      
        "name": "Thiamin (B1)",
        "amount": 0.12,
        "unit": "mg"
      ,
      
        "name": "Riboflavin (B2)",
        "amount": 0.29,
        "unit": "mg"
      ,
      
        "name": "Niacin (B3)",
        "amount": 0.75,
        "unit": "mg"
      ,
      
        "name": "Pantothenic acid (B5)",
        "amount": 0.67,
        "unit": "mg"
      ,
      
        "name": "Folate",
        "amount": 11.98,
        "unit": "mcg"
      ,
      
        "name": "Choline",
        "amount": 75.46,
        "unit": "mg"
      ,
      
        "name": "Vitamin C",
        "amount": 0,
        "unit": "mg"
      
    ],
    "total_daily_calories": "221.94",
    "weight": 3.1,
    "breed": 
      "id": 1,
      "creator_id": null,
      "updater_id": null,
      "deleter_id": null,
      "deleted_at": null,
      "giant": null,
      "name": "Abyssinian San Terrier",
      "created_at": "2020-09-23T18:38:45.675+09:30",
      "updated_at": "2020-09-23T18:38:45.675+09:30"
    
  
 ```

【问题讨论】:

这能回答你的问题吗? How to decode JSON in Flutter? 见flutter.dev/docs/development/data-and-backend/json 在你给我的参考资料中。这很有帮助。但是有没有更好的方法来做到这一点 void setReceivedText(String text) Map jsonInput = jsonDecode(text); _receivedText = 'ID: ' + jsonInput['id'] + '\n'; _receivedText += '日期:' +jsonInput['timestamp']+ '\n'; ....... 【参考方案1】:

您可以在下面复制粘贴运行完整代码 可以先隐蔽到模型,详细可以看完整代码 代码sn-p

Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));

String payloadToJson(Payload data) => json.encode(data.toJson());

class Payload 
  Payload(
    this.success,
    this.pet,
  );

  bool success;
  Pet pet;

  factory Payload.fromJson(Map<String, dynamic> json) => Payload(
        success: json["success"],
        pet: Pet.fromJson(json["pet"]),
      );

  Map<String, dynamic> toJson() => 
        "success": success,
        "pet": pet.toJson(),
      ;


class Pet 
  Pet(
    this.id,
    this.activityLevelName,
    this.dailyAllowanceGuidelinesDetails,
    this.eatbone,
    this.idealWeight,
    this.image,
    this.name,
    this.nutrientGuidelineDetail,
    this.totalDailyCalories,
    this.weight,
    this.breed,
  );

  int id;
  String activityLevelName;
  List<DailyAllowanceGuidelinesDetail> dailyAllowanceGuidelinesDetails;
  bool eatbone;
  double idealWeight;
  Image image;
  String name;
  List<NutrientGuidelineDetail> nutrientGuidelineDetail;
  String totalDailyCalories;
  double weight;
  Breed breed;

  factory Pet.fromJson(Map<String, dynamic> json) => Pet(
        id: json["id"],
        activityLevelName: json["activity_level_name"],
        dailyAllowanceGuidelinesDetails:
            List<DailyAllowanceGuidelinesDetail>.from(
                json["daily_allowance_guidelines_details"]
                    .map((x) => DailyAllowanceGuidelinesDetail.fromJson(x))),
        eatbone: json["eatbone"],
        idealWeight: json["ideal_weight"].toDouble(),
        image: Image.fromJson(json["image"]),
        name: json["name"],
        nutrientGuidelineDetail: List<NutrientGuidelineDetail>.from(
            json["nutrient_guideline_detail"]
                .map((x) => NutrientGuidelineDetail.fromJson(x))),
        totalDailyCalories: json["total_daily_calories"],
        weight: json["weight"].toDouble(),
        breed: Breed.fromJson(json["breed"]),
      );

  Map<String, dynamic> toJson() => 
        "id": id,
        "activity_level_name": activityLevelName,
        "daily_allowance_guidelines_details": List<dynamic>.from(
            dailyAllowanceGuidelinesDetails.map((x) => x.toJson())),
        "eatbone": eatbone,
        "ideal_weight": idealWeight,
        "image": image.toJson(),
        "name": name,
        "nutrient_guideline_detail":
            List<dynamic>.from(nutrientGuidelineDetail.map((x) => x.toJson())),
        "total_daily_calories": totalDailyCalories,
        "weight": weight,
        "breed": breed.toJson(),
      ;


class Breed 
  Breed(
    this.id,
    this.creatorId,
    this.updaterId,
    this.deleterId,
    this.deletedAt,
    this.giant,
    this.name,
    this.createdAt,
    this.updatedAt,
  );

  int id;
  dynamic creatorId;
  dynamic updaterId;
  dynamic deleterId;
  dynamic deletedAt;
  dynamic giant;
  String name;
  DateTime createdAt;
  DateTime updatedAt;

  factory Breed.fromJson(Map<String, dynamic> json) => Breed(
        id: json["id"],
        creatorId: json["creator_id"],
        updaterId: json["updater_id"],
        deleterId: json["deleter_id"],
        deletedAt: json["deleted_at"],
        giant: json["giant"],
        name: json["name"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => 
        "id": id,
        "creator_id": creatorId,
        "updater_id": updaterId,
        "deleter_id": deleterId,
        "deleted_at": deletedAt,
        "giant": giant,
        "name": name,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
      ;


class DailyAllowanceGuidelinesDetail 
  DailyAllowanceGuidelinesDetail(
    this.name,
    this.bones,
    this.percentage,
  );

  String name;
  bool bones;
  int percentage;

  factory DailyAllowanceGuidelinesDetail.fromJson(Map<String, dynamic> json) =>
      DailyAllowanceGuidelinesDetail(
        name: json["name"],
        bones: json["bones"],
        percentage: json["percentage"],
      );

  Map<String, dynamic> toJson() => 
        "name": name,
        "bones": bones,
        "percentage": percentage,
      ;


class Image 
  Image(
    this.url,
    this.thumb,
  );

  dynamic url;
  Thumb thumb;

  factory Image.fromJson(Map<String, dynamic> json) => Image(
        url: json["url"],
        thumb: Thumb.fromJson(json["thumb"]),
      );

  Map<String, dynamic> toJson() => 
        "url": url,
        "thumb": thumb.toJson(),
      ;


class Thumb 
  Thumb(
    this.url,
  );

  dynamic url;

  factory Thumb.fromJson(Map<String, dynamic> json) => Thumb(
        url: json["url"],
      );

  Map<String, dynamic> toJson() => 
        "url": url,
      ;


class NutrientGuidelineDetail 
  NutrientGuidelineDetail(
    this.name,
    this.amount,
    this.unit,
  );

  String name;
  double amount;
  Unit unit;

  factory NutrientGuidelineDetail.fromJson(Map<String, dynamic> json) =>
      NutrientGuidelineDetail(
        name: json["name"],
        amount: json["amount"].toDouble(),
        unit: unitValues.map[json["unit"]],
      );

  Map<String, dynamic> toJson() => 
        "name": name,
        "amount": amount,
        "unit": unitValues.reverse[unit],
      ;


enum Unit  G, RATIO, MG, MCG 

final unitValues = EnumValues(
    "g": Unit.G, "mcg": Unit.MCG, "mg": Unit.MG, "ratio": Unit.RATIO);

class EnumValues<T> 
  Map<String, T> map;
  Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse 
    if (reverseMap == null) 
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    
    return reverseMap;
  


void main() 
  runApp(MyApp());


class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  


class MyHomePage extends StatefulWidget 
  MyHomePage(Key key, this.title) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  Future<Payload> _future;

  Future<Payload> getData() async 
    String jsonString = '''
    "success": true,
  "pet": 
    "id": 17,
    "activity_level_name": "Low Activity (<1hr/day)",
    "daily_allowance_guidelines_details": [
      
        "name": "Muscle Meat",
        "bones": true,
        "percentage": 60
      ,
      
        "name": "Organ Meat",
        "bones": false,
        "percentage": 10
      ,
      
        "name": "Fruit and Veg",
        "bones": false,
        "percentage": 20
      ,
      
        "name": "Bone",
        "bones": true,
        "percentage": 10
      
    ],
    "eatbone": true,
    "ideal_weight": 3.3,
    "image": 
      "url": null,
      "thumb": 
        "url": null
      
    ,
    "name": "Roxy",
    "nutrient_guideline_detail": [
      
        "name": "Protein",
        "amount": 9.99,
        "unit": "g"
      ,
      
        "name": "Crude fat",
        "amount": 3.06,
        "unit": "g"
      ,
      
        "name": "Calcium/Phosphorus ratio",
        "amount": 0.44,
        "unit": "ratio"
      ,
      
        "name": "Omega-3/6 ratio",
        "amount": 0.06,
        "unit": "ratio"
      ,
      
        "name": "Omega-6",
        "amount": 0.62,
        "unit": "g"
      ,
      
        "name": "Omega-3 excl. ALA and SDA",
        "amount": 0.02,
        "unit": "g"
      ,
      
        "name": "Calcium",
        "amount": 0.28,
        "unit": "g"
      ,
      
        "name": "Phosphorus",
        "amount": 0.22,
        "unit": "g"
      ,
      
        "name": "Potassium",
        "amount": 0.33,
        "unit": "g"
      ,
      
        "name": "Sodium (Na)",
        "amount": 0.04,
        "unit": "mg"
      ,
      
        "name": "Magnesium",
        "amount": 0.03,
        "unit": "mg"
      ,
      
        "name": "Iron",
        "amount": 2.22,
        "unit": "mg"
      ,
      
        "name": "Copper",
        "amount": 0.41,
        "unit": "mg"
      ,
      
        "name": "Manganese",
        "amount": 0.28,
        "unit": "mg"
      ,
      
        "name": "Zinc (Zn)",
        "amount": 4.44,
        "unit": "mg"
      ,
      
        "name": "Iodine",
        "amount": 55.49,
        "unit": "mcg"
      ,
      
        "name": "Selenium",
        "amount": 0.02,
        "unit": "mcg"
      ,
      
        "name": "Vitamin A",
        "amount": 83.23,
        "unit": "mcg"
      ,
      
        "name": "Vitamin D",
        "amount": 0.69,
        "unit": "mcg"
      ,
      
        "name": "Vitamin E",
        "amount": 2.5,
        "unit": "mg"
      ,
      
        "name": "Thiamin (B1)",
        "amount": 0.12,
        "unit": "mg"
      ,
      
        "name": "Riboflavin (B2)",
        "amount": 0.29,
        "unit": "mg"
      ,
      
        "name": "Niacin (B3)",
        "amount": 0.75,
        "unit": "mg"
      ,
      
        "name": "Pantothenic acid (B5)",
        "amount": 0.67,
        "unit": "mg"
      ,
      
        "name": "Folate",
        "amount": 11.98,
        "unit": "mcg"
      ,
      
        "name": "Choline",
        "amount": 75.46,
        "unit": "mg"
      ,
      
        "name": "Vitamin C",
        "amount": 0,
        "unit": "mg"
      
    ],
    "total_daily_calories": "221.94",
    "weight": 3.1,
    "breed": 
      "id": 1,
      "creator_id": null,
      "updater_id": null,
      "deleter_id": null,
      "deleted_at": null,
      "giant": null,
      "name": "Abyssinian San Terrier",
      "created_at": "2020-09-23T18:38:45.675+09:30",
      "updated_at": "2020-09-23T18:38:45.675+09:30"
    
  
 
    ''';
    http.Response response = http.Response(jsonString, 200);
    if (response.statusCode == 200) 
      return payloadFromJson(response.body);
     else 
      throw Exception();
    
  

  @override
  void initState() 
    _future = getData();
    super.initState();
  

  @override
  Widget build(BuildContext context) 
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: FutureBuilder(
            future: _future,
            builder: (context, AsyncSnapshot<Payload> snapshot) 
              switch (snapshot.connectionState) 
                case ConnectionState.none:
                  return Text('none');
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                case ConnectionState.active:
                  return Text('');
                case ConnectionState.done:
                  if (snapshot.hasError) 
                    return Text(
                      '$snapshot.error',
                      style: TextStyle(color: Colors.red),
                    );
                   else 
                    return Column(
                      children: [
                        Text("Name $snapshot.data.pet.name"),
                        ListView.builder(
                            shrinkWrap: true,
                            itemCount: snapshot.data.pet
                                .dailyAllowanceGuidelinesDetails.length,
                            itemBuilder: (context, index) 
                              return Card(
                                  elevation: 6.0,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 6.0,
                                        bottom: 6.0,
                                        left: 8.0,
                                        right: 8.0),
                                    child: Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Text(snapshot
                                            .data
                                            .pet
                                            .dailyAllowanceGuidelinesDetails[
                                                index]
                                            .name
                                            .toString()),
                                        Spacer(),
                                        Text(
                                          snapshot
                                              .data
                                              .pet
                                              .dailyAllowanceGuidelinesDetails[
                                                  index]
                                              .percentage
                                              .toString(),
                                        ),
                                      ],
                                    ),
                                  ));
                            ),
                      ],
                    );
                  
              
            ));
  

【讨论】:

嘿,感谢您的意见。它的分类很好而且很有帮助。在我的 api 方法调用中, Future createThePet(data, apiUrl) async var fullUrl = _baseUrl + apiUrl; // + 等待 _getToken();最终响应 = 等待 http.post(fullUrl, body: jsonEncode(data), headers: _setHeaders()); if(response.statusCode == 200) final String responseString = response.body;打印(响应字符串);返回 petCreateFromJson(responseString); else 返回空值;这个“返回 petCreateFromJson(responseString);”不返回,我做错了什么? @JasonWaku,您能否将复制代码发布到一个新问题。谢谢。 嘿,当我尝试解码 API 的其余部分时,在 ""nutrient_guideline_detail": [" 部分我收到此错误 Unhandled Exception: type 'List' is not a subtype of在模型工厂方法 nutrientGuidelines 中输入“Map”:List.from(json["nutrient_guidelines"].map((x) => NutrientGuideline.fromJson(x))),我在做这个错了吗? @JasonWaku,您能否将您的复制代码发布到一个新问题并ping 我的名字,谢谢。

以上是关于将 ison 转换为可读的飞镖的主要内容,如果未能解决你的问题,请参考以下文章

将时间(自 YYYY 以来的天数给出)转换为可读的时间格式

将分钟转换为可读的天/小时/分钟

如何使用 XSLT 将刻度转换为可读的日期时间?

将 dataFrame 转换为可读的,以便使用 plotly express 绘制条形图

Power Automate Flow:将 json 转换为可读的 PowerAutomate-Items

如何在 Swift 中将多字符数字格式转换为可读的字符串?