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
342 views
in Technique[技术] by (71.8m points)

flutter - convert json to dart

I am a beginner in Flutter, I just want to know how to print productA and 220 in console. The below is json file and also create dart file below.

{
  "status": true,
  "message": "Data returned successfull",
  "data": {
    "products": [
      {
        "productName": "productA",
        "productPrice": "220.00",
      }
    ]
  }
}

Product.dart

class Product {
  bool status;
  String message;
  Data data;

  Product({this.status, this.message, this.data});

  Product.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }
}

class Data {
  List<Products> products;

  Data({this.products});

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

class Products {
  String prodName;
  String prodRkPrice;

  Products({this.prodName, this.prodRkPrice});

  Products.fromJson(Map<String, dynamic> json) {
    prodName = json['prodName'];
    prodRkPrice = json['prodRkPrice'];
  }
}




But still don't know how to print those values.


fetchData() async {
    try {
      String extractedData =
          await http.get('json url').toString();

      final parsed = jsonDecode(extractedData).cast(Map<String, dynamic>());

      final products = Product.fromJson(parsed);

      print(products);

      //print(json.decode(response.body));
      //print(response[0]);
    } catch (error) {
      throw (error);
    }
  }


I have tried using this method but getting errors, don't know how to parse and print those values? Please help me I am a beginner in Flutter

question from:https://stackoverflow.com/questions/65598298/convert-json-to-dart

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

1 Answer

0 votes
by (71.8m points)

Change your fetchData to:

Future<void> fetchData() async {
  try {
    Response response = await http.get('json url'); //getting the response without .toString
    Map<String, dynamic> extractedData = jsonDecode(response.body); //converting the response

    Product products = Product.fromJson(extractedData);

    products.data.products.forEach((product) {
      print(product.prodName);
      print(product.prodRkPrice);
    });

  } catch (error) {
    throw (error);
  }
}

Just remember to match the same attribute names of the api in the Products class:

Products.fromJson(Map<String, dynamic> json) {
    prodName = json['productName'];
    prodRkPrice = json['productPrice'];
}

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

2.1m questions

2.1m answers

60 comments

56.9k users

...