简体   繁体   中英

How to parse the JSON response from URL in flutter

I am new to flutter and want to parse the data from a URL that is in json format. The url that I am using is json link . I want to get the "verse" and "chapter" fields from the json array. I have succeeded in getting the response body in snackbar but not able to get single values. I want to get the "verse" and "chapter" and show then in text box. I am using Dart.

Here is the method that I am using:

 _makeGetRequest() async {
    // make request
    var url = Uri.parse("http://quotes.rest/bible/vod.json");
    Response response = await http.get(url);

    // sample info available in response
    int statusCode = response.statusCode;
    Map<String, String> headers = response.headers;
    String contentType = headers['content-type'];
    String json = response.body;

    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(json)));

  }

 @override
  void initState() {
    super.initState();
    streamingController.config(url: "http://stream.zeno.fm/qa8p6uz2tk8uv");
    streamingController.play();
    _makeGetRequest();
  }

Please help me in getting the proper field values and set them in text box as I am trying to solve it from past two days and have tried all solutions from internet but getting exceptions.

use jsonDecode

 Map<String,dynamic> data = jsonDecode(response.body);
 String verse = data["contents"]["verse"];
 dynamic chapter= data["contents"]["chapter"];

however I recommend modeling your data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM