gpt4 book ai didi

json - 将 json 响应转换为字符串并将其放入结构中

转载 作者:IT王子 更新时间:2023-10-29 06:52:39 25 4
gpt4 key购买 nike

我正在制作一个用于购物的移动应用

我想用来自 json 的在线数据替换静态数据,并将这些数据放在一个结构中以使用 Gridview 显示它并在其他地方使用它所以这是结构:

    class Product {

String _urlToImage;
String _about;
String _title;
double _price;
double _weight;
int _id;


Product(this._urlToImage, this._title, this._price, this._weight, this._id){
_about = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
}

double get weight => _weight;

double get price => _price;

String get title => _title;

String get urlToImage => _urlToImage;

int get id => _id;

String get about => _about;


}
```

这是结构中的本地数据

  class ProductsRepository{

List<Product> fetchAllProducts() {

return [
new Product("assets/images/spelt_noodles.png", "Biona Organic Spelt Noodles", 2.99, 250, 0),
new Product("assets/images/spelt_italian.png", "Biona Organic Spelt Fusili Brown", 2.35, 500, 1),
new Product("assets/images/spelt_spaghetti.png", "Biona Organic Whole Spelt Spaghetti", 2.35, 500, 2),
new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 3),
new Product("assets/images/spelt_penne.png", "Biona Organic Whole Spelt Penne", 2.35, 500, 4),
new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 5),
new Product("assets/images/spelt_fusilli.png", "Biona Organic Spelt Fusilli Tricolore", 1.99, 250, 6),
];}

我试过了

  _fetchData() async {

final response =
await http.get("https://jsonplaceholder.typicode.com/photos");
if (response.statusCode == 200) {
list = json.decode(utf8.decode(response.bodyBytes)) as List;

} else {
throw Exception('Failed to load photos');
}
}

就像这个概念,但 json 数据应该在 Product() 结构中

List data;

Future<String> getData() async {
var response = await http.get(
Uri.encodeFull("https://aflam4app.de/JSON/" + widget.thisJson +".json"),
headers: {
'Content-Type': 'application/json',
"Accept": "application/json"
}
);

this.setState(() {
data = json.decode(utf8.decode(response.bodyBytes));
});
return "Success!";
}


@override
void initState() {
super.initState();
this.getData();
}

@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: data == null ? 0 : data.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: Single_prod(
prod_name: data[index]['name'],
prod_pricture: data[index]['picture'],
prod_link: data[index]['link'],
prod_description: data[index]['description'],
prod_type: data[index]['type'],
),
);
});
}
}

class Single_prod extends StatelessWidget {
final prod_name;
final prod_pricture;
final prod_link;
final prod_description;
final prod_type;

Single_prod({
this.prod_name,
this.prod_pricture,
this.prod_link,
this.prod_description,
this.prod_type,
});

这是我的 json 文件:

[
{
"urlToImage": "assets/images/spelt_noodles.png",
"title": "Biona Organic Spelt Noodles",
"price": 2.99,
"weight": 250,
"id": 1,
"created_at": "2019-07-07 10:44:53",
"updated_at": "2019-07-07 10:44:53"
},
{
"urlToImage": "assets/images/spelt_noodles.png",
"title": "Biona Organic Spelt Noodles",
"price": 2.99,
"weight": 250,
"id": 2,
"created_at": "2019-07-07 10:44:53",
"updated_at": "2019-07-07 10:44:53"
},
{
"urlToImage": "assets/images/spelt_noodles.png",
"title": "Biona Organic Spelt Noodles",
"price": 2.99,
"weight": 250,
"id": 3,
"created_at": "2019-07-07 10:44:53",
"updated_at": "2019-07-07 10:44:53"
},


]

最佳答案

这是解析数据的方式

    List<Product> list = new List<Product>();
var map= json.decode(response.body);

for(var item in map){
Product product = new Product.fromJson(item); //making an assumption how your json looks
list.add(product); //user a Futurebuilder to create your GridView
}

这是定义模型类的方式

    Product fromJson(Map<String, dynamic> json){
Product product = new Product(
_urlToImage = json['urlToImage'] as String;
_about = json['about'] as String;
_title = json['title'] as String;
_price = json['price'] as double;
_weight = json['weight'] as double;
_id = json['id'] as int;
);
return product;
}

...

Here GridView 示例

关于json - 将 json 响应转换为字符串并将其放入结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56953343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com