1.json字符串转map
json.decode(strData)
2.map转json字符串
json.encode(mapData)
需要引入 dart:convert 这个库
http 第三方库 自己看就好了
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
class MyRequest extends StatelessWidget {
_getRequest() async{
var url = "http://route.showapi.com/1211-1";
var response = await http.get(url);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
var itemCount = jsonResponse['totalItems'];
print('Number of books about http: $jsonResponse.');
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("请求"),
),
body: Column(
children: <Widget>[
RaisedButton(
color: Colors.red,
onPressed: _getRequest
),
RaisedButton.icon(
onPressed: (){
},
label: Text("data"),
icon: Icon(Icons.save),
)
],
),
);
}
}
网友评论