如何从颤振中的 json 响应中读取 json 数组?我是颤振的新手请帮助我

时间:2021-05-18 16:16:02

标签: json flutter http

下面是我的 json 响应,我想将此响应放入模型和嵌套模型类中。请帮我阅读这个json。提前致谢。我是 flutter 的新手,用于解析模型类中的 json 响应。

'''
{
"success": 1,
"totalResults": 5,
"data": [
     {
        "id": 1,
        "bank_name": "SBI bank",
        "bank_logo": "http://3.143.33.201/assets/banklogo/1/sbi-round-logo.png",
        "bank_desc": "sbi",
        "rating": "AA"
    },
    {
        "id": 2,
        "bank_name": "Yes bank",
        "bank_logo": "http://3.143.33.201/assets/banklogo/2/yes-round-logo.png",
        "bank_desc": "Yes bank",
        "rating": "AA"
    },
    {
        "id": 3,
        "bank_name": "Union bank",
        "bank_logo": "http://3.143.33.201/assets/banklogo/3/union-round-logo.png",
        "bank_desc": "union bank",
        "rating": "AA"
    },
    {
        "id": 9,
        "bank_name": "Bank of Baroda",
        "bank_logo": "http://3.143.33.201/assets/banklogo/9/bob.png",
        "bank_desc": "Bank of Baroda",
        "rating": "AA"
    },
    {
        "id": 10,
        "bank_name": "IndusInd Bank",
        "bank_logo": "http://3.143.33.201/assets/banklogo/10/Indus-bank-2.png",
        "bank_desc": "IndusInd Bank",
        "rating": "AA"
    }
]

}

'''

2 个答案:

答案 0 :(得分:0)

我们有一个内置的 dart 包,

dart:convert,用法如下

import 'dart:convert' as convert;
...
// perform your http request and get the response from it,
var responseData = convert.jsonDecode(response.body);

convert.jsonDecode() 将允许使用键值访问 responseData 中的数据存储。

@note :为了方便起见,我已将其导入为 convert,因为我在开发过程中不会感到困惑。

快乐编码

答案 1 :(得分:0)

您可以为此使用 JSON and serialization

db_conn = pymysql.connect(
    host=db_creds['host'],
    user=db_creds['user'],
    passwd=db_creds['passwd'],
    db=db_creds['db'],
    charset=db_creds['charset'],
    ssl_ca='C:/SSL_CERTS/ca-cert.pem',
    ssl_key='C:/SSL_CERTS/client-key.pem',
    ssl_cert='C:/SSL_CERTS/client-cert.pem'
)

之后,您可以像这样在模型类中使用 final _response = jsonDecode(jsonResponse); 数据

_response

初始化你的模型并像这样使用

YourModel.fromJson(_response);