如何在颤动中解析此响应,并从 API 响应中的两个数组的文本字段中获取响应值? [关闭]

Posted

技术标签:

【中文标题】如何在颤动中解析此响应,并从 API 响应中的两个数组的文本字段中获取响应值? [关闭]【英文标题】:How can I parse this response in flutter and have values from response in text field from both the arrays in API response? [closed] 【发布时间】:2018-11-20 19:45:48 【问题描述】:

我正在使用 Flutter/Dart。 This endpoint 给了我这个回复:

"listroute":["id":"1","route_title":"***lyn East","no_of_vehicle":null,"fare":"101.00","note":null,"is_active":"no","created_at":"2018-06-02 12:49:21","updated_at":"0000-00-00 00:00:00","vehicles":["vec_route_id":"1","id":"1","vehicle_no":"101","vehicle_model":"101","manufacture_year":"2000","driver_name":"arvind","driver_licence":"544575854","driver_contact":"1305442045","note":"","created_at":"2018-06-02 12:54:13"],"id":"2","route_title":"***lyn South","no_of_vehicle":null,"fare":"1010.00","note":null,"is_active":"no","created_at":"2018-06-02 12:50:06","updated_at":"0000-00-00 00:00:00","vehicles":["vec_route_id":"3","id":"3","vehicle_no":"2145","vehicle_model":"425","manufacture_year":"2017","driver_name":"amit","driver_licence":"4526320125","driver_contact":"23054420456","note":"","created_at":"2018-06-02 12:57:23","vec_route_id":"2","id":"2","vehicle_no":"1010","vehicle_model":"242","manufacture_year":"2016","driver_name":"abhisekh","driver_licence":"4526320125","driver_contact":"23054420456","note":"","created_at":"2018-06-02 12:56:41"],"id":"3","route_title":"***lyn West","no_of_vehicle":null,"fare":"100.00","note":null,"is_active":"no","created_at":"2018-06-02 12:51:00","updated_at":"0000-00-00 00:00:00","vehicles":[],"id":"4","route_title":"Uppal","no_of_vehicle":null,"fare":"101.00","note":null,"is_active":"no","created_at":"2018-06-02 12:51:22","updated_at":"0000-00-00 00:00:00","vehicles":["vec_route_id":"6","id":"3","vehicle_no":"2145","vehicle_model":"425","manufacture_year":"2017","driver_name":"amit","driver_licence":"4526320125","driver_contact":"23054420456","note":"","created_at":"2018-06-02 12:57:23","vec_route_id":"5","id":"2","vehicle_no":"1010","vehicle_model":"242","manufacture_year":"2016","driver_name":"abhisekh","driver_licence":"4526320125","driver_contact":"23054420456","note":"","created_at":"2018-06-02 12:56:41","vec_route_id":"4","id":"1","vehicle_no":"101","vehicle_model":"101","manufacture_year":"2000","driver_name":"arvind","driver_licence":"544575854","driver_contact":"1305442045","note":"","created_at":"2018-06-02 12:54:13"],"id":"5","route_title":"MAHE","no_of_vehicle":null,"fare":"101.00","note":null,"is_active":"no","created_at":"2018-06-02 12:51:37","updated_at":"0000-00-00 00:00:00","vehicles":["vec_route_id":"7","id":"2","vehicle_no":"1010","vehicle_model":"242","manufacture_year":"2016","driver_name":"abhisekh","driver_licence":"4526320125","driver_contact":"23054420456","note":"","created_at":"2018-06-02 12:56:41"],"id":"6","route_title":"CMD Chowk","no_of_vehicle":null,"fare":"200.00","note":null,"is_active":"no","created_at":"2018-06-02 12:51:58","updated_at":"0000-00-00 00:00:00","vehicles":[],"id":"9","route_title":"x","no_of_vehicle":null,"fare":"10.00","note":null,"is_active":"no","created_at":"2018-06-11 07:22:46","updated_at":"0000-00-00 00:00:00","vehicles":[],"id":"10","route_title":"xy","no_of_vehicle":null,"fare":"10.00","note":null,"is_active":"no","created_at":"2018-06-11 07:22:53","updated_at":"0000-00-00 00:00:00","vehicles":[],"id":"11","route_title":"mmm","no_of_vehicle":null,"fare":"10.00","note":null,"is_active":"no","created_at":"2018-06-11 11:48:02","updated_at":"0000-00-00 00:00:00","vehicles":[]],"childs":["transport_fees":"0.00","vehroute_id":"6","student_session_id":"1","fees_discount":"0.00","class_id":"1","class":"Nursery A","section_id":"1","section":"A","id":"1","admission_no":"1","roll_no":"01052018","admission_date":"2018-05-30","firstname":"Amit","lastname":"Teja","image":"uploads\/student_images\/no_image.png","mobileno":"09021902190","email":"amit@mailinator.com","state":null,"city":null,"pincode":null,"religion":"Teja","cast":"Gen","dob":"2016-07-06","current_address":"ttetet\r\nasdasdasd","previous_school":"","guardian_is":"father","permanent_address":"ttetet\r\nasdasdasd","category_id":"1","adhar_no":"iewk123kaqws12","samagra_id":"iewk123kaqws12","bank_account_no":"9999999999999999999999","bank_name":"CITY Bank","ifsc_code":"city000010","guardian_name":"Sumit","guardian_relation":"Father","guardian_phone":"8309632153","guardian_address":"ttetet\r\nasdasdasd","is_active":"no","created_at":"2018-06-06 11:52:43","updated_at":"0000-00-00 00:00:00","father_name":"Sumit","father_phone":"Teja","father_occupation":"Business","mother_name":"Sumitra","mother_phone":"9876543211","mother_occupation":"House Wife","guardian_occupation":"Business","gender":"Male","rte":"Yes","guardian_email":"sumit@mailinato.com","transport_fees":"0.00","vehroute_id":"0","student_session_id":"2","fees_discount":"0.00","class_id":"1","class":"Nursery A","section_id":"1","section":"A","id":"2","admission_no":"2","roll_no":"01052019","admission_date":"2018-06-01","firstname":"vibhav","lastname":"Teja","image":"uploads\/student_images\/no_image.png","mobileno":"7894561230","email":"vibhav@gmail.com","state":null,"city":null,"pincode":null,"religion":"Teja","cast":"Gen","dob":"2018-06-09","current_address":"ttetet\r\nasdasdasd","previous_school":"","guardian_is":"father","permanent_address":"ttetet\r\nasdasdasd","category_id":"1","adhar_no":"iewk123kaqws12","samagra_id":"iewk123kaqws12","bank_account_no":"125423625642","bank_name":"City Bank","ifsc_code":"12542362","guardian_name":"Sumit","guardian_relation":"Father","guardian_phone":"8309632153","guardian_address":"ttetet\r\nasdasdasd","is_active":"no","created_at":"2018-06-06 11:29:26","updated_at":"0000-00-00 00:00:00","father_name":"Sumit","father_phone":"Teja","father_occupation":"Business","mother_name":"Sumitra","mother_phone":"9876543211","mother_occupation":"House Wife","guardian_occupation":"Business","gender":"Male","rte":"Yes","guardian_email":"sumit@mailinato.com","transport_fees":"0.00","vehroute_id":"6","student_session_id":"11","fees_discount":"0.00","class_id":"2","class":"Nursery B","section_id":"2","section":"B","id":"5","admission_no":"1234-A1","roll_no":"0105202010","admission_date":"2018-06-11","firstname":"Rajat","lastname":"Teja","image":"uploads\/student_images\/no_image.png","mobileno":"07697019875","email":"rajat@gmail.com","state":null,"city":null,"pincode":null,"religion":"Teja","cast":"","dob":"2018-06-14","current_address":"ttetet\r\nasdasdasd","previous_school":"","guardian_is":"father","permanent_address":"ttetet\r\nasdasdasd","category_id":"3","adhar_no":"","samagra_id":"","bank_account_no":"","bank_name":"","ifsc_code":"","guardian_name":"Sumit","guardian_relation":"Father","guardian_phone":"8309632153","guardian_address":"ttetet\r\nasdasdasd","is_active":"no","created_at":"2018-06-11 07:35:54","updated_at":"0000-00-00 00:00:00","father_name":"Sumit","father_phone":"Teja","father_occupation":"Business","mother_name":"Sumitra","mother_phone":"9876543211","mother_occupation":"House Wife","guardian_occupation":"Business","gender":"Female","rte":"No","guardian_email":""]

如何解析这种类型的输出?

【问题讨论】:

你尝试了什么? 欢迎来到 ***。在提出问题时,如果您提供代码,您将获得更好的答案。添加您迄今为止尝试过的内容。另请查看How to create a Minimal, Complete, and Verifiable example 您应该向我们提供示例或至少您尝试过的内容,以便我们能够提供帮助。不管你有没有试过这个:flutter.io/json? 【参考方案1】:

如果是字符串,可以使用jsonDecodejson.decode

import 'dart:convert' show jsonDecode, json;

Map<String,dynamic> parseResponse(String response) 
  return jsonDecode(response);

【讨论】:

【参考方案2】:

问题中的数据似乎已被解析。可能您想知道如何使用接收到的数据。这是一个例子。

class HomeState extends State<Home> 
List listRoute = [];

Future getData() async 
    http.Response res = await http.get(
        'http://app.webcraft.co.in/eschool/parent/routejson?parent_id=2&student_id=1');
    Map mapRes = json.decode(res.body);
    print('Response from server: $mapRes');
    setState(() 
    listRoute = mapRes['listroute'];
    );


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


@override
Widget build(BuildContext context) 
    return new Scaffold(
    appBar: new AppBar(
        title: new Text('HTTP demo'),
    ),
    body: listRoute.length == 0
        ? new Container()
        : new ListView(
            children: listRoute
                .map((route) => new ListTile(
                    title: new Text(route['route_title']),
                    subtitle: new Text(route['fare'])))
                .toList(),
            ),
    );


或者您可能需要了解,您收到的是Map,其中键listroute 的值是您需要迭代的列表。

如果您无法访问地图中的某些其他键或数据,请在评论中询问。

注意:如果您无法理解/读取数据,有几个站点可以查看漂亮格式的 json 数据,例如 http://json.parser.online.fr/

【讨论】:

嘿,谢谢,我需要解析 listroute 和 vihicles 数组值的值并将其用于文本 我还需要在文本中显示车辆值以及其他 listroutes 值 @AyushJainWebCraftIT 收到此错误 - 未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”。 E/flutter (24765):接收者:null E/flutter (24765):尝试调用:[]("BankName") 在我的情况下,没有列出在 textview 中获取的响应,但每次都出现此错误

以上是关于如何在颤动中解析此响应,并从 API 响应中的两个数组的文本字段中获取响应值? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在颤动中显示来自api响应的png图像?

在颤动中没有得到正确的 api 响应

解析json数组响应/颤动

将 Json 响应中的对象列表映射到颤动中的列表

如何在颤动中返回 POST 响应对象?

如何在php中解析soap xml响应并从字符串中获取信息