Home > OS >  How to extract data from HTTP response. Flutter/Dart
How to extract data from HTTP response. Flutter/Dart

Time:07-27

How do I extract the items in "run_hyperlink"

this is the response that I am getting:

"ListNode":[
        {
          "id":"2",
          "mad_key":"32835",
          "is_custom":"0",
          "is_locked":"0",
          "is_inactive":"1",
          "run_hyperlink":{
            "classid":"25510",
            "id":"2",
            "mad_key":"32835"
          },
          "field":[
            {
              "field_name":"code",
              "col_index":"1",
              "field_value":"LE-0000000002",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"common_desc_0",
              "col_index":"2",
              "field_value":"test_01",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"id_Org",
              "col_index":"3",
              "field_value":"01_01_04_01_SA - Shah Alam",
              "mad_key":"100377",
              "id":"100055"
            },
            {
              "field_name":"dateReported",
              "col_index":"4",
              "field_value":"18/09/2020",
              "mad_key":"0",
              "id":"0"
            }
          ]
        },

and this is the code that I am using to get the response :

final data = jsonDecode(response.body);

print(data['RESPONSE']['GenericListAnswer']['ListNode']);

I just want to extract the "run_hyperlink" and will identify my individual list tiles later on.

Preferably In a list form like :

"classid":"25510",
"id:"2",
"mad_key":"32835"

CodePudding user response:

If I understand your question well, here is simply the anwer:

final data = jsonDecode(response.body);
final listNode = data['RESPONSE']['GenericListAnswer']['ListNode'];
final list_ = [];

for (element in listNode){
list_.append(element);
}
print(list_); // ["classid":"25510", "id":"2", "mad_key":"32835"]

CodePudding user response:

Use map to pick up the required parts of a list item.

final hyperlinks = data['RESPONSE']['GenericListAnswer']['ListNode']
  .map((i) => i['run_hyperlink'])
  .toList().cast<Map<String, dynamic>>();

CodePudding user response:

this is multi dimensional array

// data response convert to object
final data = jsonDecode(response.body);

// get single run_hyperlink
print(data['RESPONSE']['GenericListAnswer']['ListNode'][0]['run_hyperlink'])

// get multiple run_hyperlink
final runHyperlink = data['ListNode'].map( (e) => e['run_hyperlink']).toList();

print(runHyperlink);

// result
[{
  "classid":"25510",
  "id":"2",
  "mad_key":"32835"
}]

// type data is array / list [] , select the index start from 0 to get item,
// and by key if type data is map {}


  • Related