Home > Blockchain >  How do I extract data from this http response?
How do I extract data from this http response?

Time:07-15

I'm trying to build a "message board" in my app and these field_value are the message fields that I am trying to extract and display.

How do I extract this specific field field_value? There are 6 Users currently in the status S030 - Approved", from the HTTP response below.

As you can see this specific message is Identified by the "id":"100001" I want to extract the field_value from the block that has "field_name":"common_desc_0"

enter image description here

The actual response is extremely long and I've just shortened this so that I can post it here without exceeding the limit. This is the HTTP response:

{
  "RESPONSE":{
    "GenericListAnswer":{
      "empty_top_toolbar":"0",
      "no_move_columns":"0",
      "no_add_rem_columns":"0",
      "no_sort_columns":"0",
      "no_resize_columns":"0",
      "no_oper_columns":"0",
      "refresh_data":{
        "ApplEra":"",
        "delay":"0"
      },
      "no_gui_link":"0",
      "index_last":"11",
      "max_num_rows":"0",
      "index_true_last":"11",
      "is_history":"0",
      "index_first_visible":"1",
      "display_table_name":"Board",
      "table_id":"25018",
      "table_name":"Board",
      "global_operation":[
        {
          "id":"5013",
          "key":"show_all_records"
        },
        {
          "id":"5005",
          "key":"search"
        },
        {
          "id":"2016",
          "key":"print_list_selected"
        },
        {
          "id":"5015",
          "key":"download_csv_batch"
        },
        {
          "id":"5014",
          "key":"download_csv_gui"
        },
        {
          "id":"5016",
          "key":"download_xlsx_gui"
        },
        {
          "id":"2015",
          "key":"print_list_all"
        },
        {
          "id":"46",
          "key":"mark_as_read"
        },
        {
          "id":"47",
          "key":"mark_as_unread"
        },
        {
          "id":"42",
          "key":"create_news"
        },
        {
          "id":"3",
          "key":"view",
          "only1":"1"
        },
        {
          "id":"11",
          "key":"edit",
          "only1":"1"
        },
        {
          "id":"0",
          "key":"delete"
        },
        {
          "id":"64",
          "key":"set_valid"
        },
        {
          "id":"65",
          "key":"set_invalid"
        },
        {
          "id":"15",
          "key":"insert"
        },
        {
          "id":"132",
          "key":"clone",
          "only1":"1"
        },
        {
          "id":"84",
          "key":"set_lock"
        },
        {
          "id":"85",
          "key":"set_unlock"
        }
      ],
      "num_visible":"200",
      "list_type":"0",
      "list_key":"1",
      "parent_Class_id":"0",
      "parent_common_id":"0",
      "parent_applcol_id":"0",
      "search_num":"0",
      "id_box_search_num":"0",
      "mad_key_list":"",
      "choose_xsl":"0",
      "is_search_popup":"0",
      "section_list_params":"",
      "QuickSearch":{
        "quick_search_column":[
          "Description",
          "Description 1"
        ]
      },
      "description":"",
      "wf_configured":"0",
      "op_parameter":"",
      "ListNode":[
        {
          "id":"100001",
          "mad_key":"100001",
          "is_custom":"0",
          "is_locked":"0",
          "is_inactive":"0",
          "run_hyperlink":{
            "classid":"25018",
            "id":"100001",
            "mad_key":"100001"
          },
          "field":[
            {
              "field_name":"common_desc_0",
              "col_index":"1",
              "field_value":"There are 6 Users currently in the status S030 - Approved",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"btime_touched",
              "col_index":"2",
              "field_value":"25/09/2020 22:33:24",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"date_end",
              "col_index":"3",
              "field_value":"",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"desc_board",
              "col_index":"4",
              "field_value":"There are 6 Users currently in the status S030 - Approved",
              "mad_key":"0",
              "id":"0"
            }
          ]
        }

This is the code for the GET request :

 var response = await http.get(
        Uri.parse(
            'http://192.168.1.8:8080/HongLeong/MENU_REQUEST.do?_dc=1657717579436&table_id=25018&id_MenuAction=3&reset_context=1&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=5&activeWindowId=mw_5&noOrigUserDate=true&LocalDate=20220713&LocalTime=21061900&TimeZone=Asia/Shanghai&UserDate=0&UserTime=0&server_name=OPRISK_DATACOLLECTOR&key_id_list=&cell_context_id=0&id_Desktop=100237&operation_key=1000007&operation_sub_num=-1&is_json=1&is_popup=0&is_search_window=0&ccsfw_conf_by_user=0&is_batch=0&previousToken=1657717554097&historyToken=1657717579434&historyUrl=1'),
        headers: {HttpHeaders.cookieHeader: cookies},
      );

      final message = jsonDecode(response.body.toString());

      await WriteCache.setString(key: "cache", value: message1.toString());

      print(message);

CodePudding user response:

As I understand your question, You want to access a data which you got from HTTP.

So,You can follow below steps.

To access data of empty_top_toolbar key

print("data"   message["RESPONSE"]["GenericListAnswer"]["empty_top_toolbar"].toString());

To access id of global_operation list...

ListView.builder(
   itemCount: message["RESPONSE"]["GenericListAnswer"].["global_operation"].length,
   builder:(context, index){
       return Text("Id ${message["RESPONSE"]["GenericListAnswer"]["global_operation"][index]["id"]}");
   }
)

Same way you can access key name.

CodePudding user response:

First create a response model by converting json to dart model.

like have done this for given response data :

class ResponseModel {


RESPONSE? rESPONSE;

  ResponseModel({this.rESPONSE});

  ResponseModel.fromJson(Map<String, dynamic> json) {
    rESPONSE = json['RESPONSE'] != null
        ? new RESPONSE.fromJson(json['RESPONSE'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.rESPONSE != null) {
      data['RESPONSE'] = this.rESPONSE!.toJson();
    }
    return data;
  }
}

class RESPONSE {
  GenericListAnswer? genericListAnswer;

  RESPONSE({this.genericListAnswer});

  RESPONSE.fromJson(Map<String, dynamic> json) {
    genericListAnswer = json['GenericListAnswer'] != null
        ? new GenericListAnswer.fromJson(json['GenericListAnswer'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.genericListAnswer != null) {
      data['GenericListAnswer'] = this.genericListAnswer!.toJson();
    }
    return data;
  }
}

class GenericListAnswer {
  String? emptyTopToolbar;
  String? noMoveColumns;
  String? noAddRemColumns;
  String? noSortColumns;
  String? noResizeColumns;
  String? noOperColumns;
  RefreshData? refreshData;
  String? noGuiLink;
  String? indexLast;
  String? maxNumRows;
  String? indexTrueLast;
  String? isHistory;
  String? indexFirstVisible;
  String? displayTableName;
  String? tableId;
  String? tableName;
  List<GlobalOperation>? globalOperation;
  String? numVisible;
  String? listType;
  String? listKey;
  String? parentClassId;
  String? parentCommonId;
  String? parentApplcolId;
  String? searchNum;
  String? idBoxSearchNum;
  String? madKeyList;
  String? chooseXsl;
  String? isSearchPopup;
  String? sectionListParams;
  QuickSearch? quickSearch;
  String? description;
  String? wfConfigured;
  String? opParameter;
  List<ListNode>? listNode;

  GenericListAnswer(
      {this.emptyTopToolbar,
      this.noMoveColumns,
      this.noAddRemColumns,
      this.noSortColumns,
      this.noResizeColumns,
      this.noOperColumns,
      this.refreshData,
      this.noGuiLink,
      this.indexLast,
      this.maxNumRows,
      this.indexTrueLast,
      this.isHistory,
      this.indexFirstVisible,
      this.displayTableName,
      this.tableId,
      this.tableName,
      this.globalOperation,
      this.numVisible,
      this.listType,
      this.listKey,
      this.parentClassId,
      this.parentCommonId,
      this.parentApplcolId,
      this.searchNum,
      this.idBoxSearchNum,
      this.madKeyList,
      this.chooseXsl,
      this.isSearchPopup,
      this.sectionListParams,
      this.quickSearch,
      this.description,
      this.wfConfigured,
      this.opParameter,
      this.listNode});

  GenericListAnswer.fromJson(Map<String, dynamic> json) {
    emptyTopToolbar = json['empty_top_toolbar'];
    noMoveColumns = json['no_move_columns'];
    noAddRemColumns = json['no_add_rem_columns'];
    noSortColumns = json['no_sort_columns'];
    noResizeColumns = json['no_resize_columns'];
    noOperColumns = json['no_oper_columns'];
    refreshData = json['refresh_data'] != null
        ? new RefreshData.fromJson(json['refresh_data'])
        : null;
    noGuiLink = json['no_gui_link'];
    indexLast = json['index_last'];
    maxNumRows = json['max_num_rows'];
    indexTrueLast = json['index_true_last'];
    isHistory = json['is_history'];
    indexFirstVisible = json['index_first_visible'];
    displayTableName = json['display_table_name'];
    tableId = json['table_id'];
    tableName = json['table_name'];
    if (json['global_operation'] != null) {
      globalOperation = <GlobalOperation>[];
      json['global_operation'].forEach((v) {
        globalOperation!.add(new GlobalOperation.fromJson(v));
      });
    }
    numVisible = json['num_visible'];
    listType = json['list_type'];
    listKey = json['list_key'];
    parentClassId = json['parent_Class_id'];
    parentCommonId = json['parent_common_id'];
    parentApplcolId = json['parent_applcol_id'];
    searchNum = json['search_num'];
    idBoxSearchNum = json['id_box_search_num'];
    madKeyList = json['mad_key_list'];
    chooseXsl = json['choose_xsl'];
    isSearchPopup = json['is_search_popup'];
    sectionListParams = json['section_list_params'];
    quickSearch = json['QuickSearch'] != null
        ? new QuickSearch.fromJson(json['QuickSearch'])
        : null;
    description = json['description'];
    wfConfigured = json['wf_configured'];
    opParameter = json['op_parameter'];
    if (json['ListNode'] != null) {
      listNode = <ListNode>[];
      json['ListNode'].forEach((v) {
        listNode!.add(new ListNode.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['empty_top_toolbar'] = this.emptyTopToolbar;
    data['no_move_columns'] = this.noMoveColumns;
    data['no_add_rem_columns'] = this.noAddRemColumns;
    data['no_sort_columns'] = this.noSortColumns;
    data['no_resize_columns'] = this.noResizeColumns;
    data['no_oper_columns'] = this.noOperColumns;
    if (this.refreshData != null) {
      data['refresh_data'] = this.refreshData!.toJson();
    }
    data['no_gui_link'] = this.noGuiLink;
    data['index_last'] = this.indexLast;
    data['max_num_rows'] = this.maxNumRows;
    data['index_true_last'] = this.indexTrueLast;
    data['is_history'] = this.isHistory;
    data['index_first_visible'] = this.indexFirstVisible;
    data['display_table_name'] = this.displayTableName;
    data['table_id'] = this.tableId;
    data['table_name'] = this.tableName;
    if (this.globalOperation != null) {
      data['global_operation'] =
          this.globalOperation!.map((v) => v.toJson()).toList();
    }
    data['num_visible'] = this.numVisible;
    data['list_type'] = this.listType;
    data['list_key'] = this.listKey;
    data['parent_Class_id'] = this.parentClassId;
    data['parent_common_id'] = this.parentCommonId;
    data['parent_applcol_id'] = this.parentApplcolId;
    data['search_num'] = this.searchNum;
    data['id_box_search_num'] = this.idBoxSearchNum;
    data['mad_key_list'] = this.madKeyList;
    data['choose_xsl'] = this.chooseXsl;
    data['is_search_popup'] = this.isSearchPopup;
    data['section_list_params'] = this.sectionListParams;
    if (this.quickSearch != null) {
      data['QuickSearch'] = this.quickSearch!.toJson();
    }
    data['description'] = this.description;
    data['wf_configured'] = this.wfConfigured;
    data['op_parameter'] = this.opParameter;
    if (this.listNode != null) {
      data['ListNode'] = this.listNode!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class RefreshData {
  String? applEra;
  String? delay;

  RefreshData({this.applEra, this.delay});

  RefreshData.fromJson(Map<String, dynamic> json) {
    applEra = json['ApplEra'];
    delay = json['delay'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['ApplEra'] = this.applEra;
    data['delay'] = this.delay;
    return data;
  }
}

class GlobalOperation {
  String? id;
  String? key;
  String? only1;

  GlobalOperation({this.id, this.key, this.only1});

  GlobalOperation.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    key = json['key'];
    only1 = json['only1'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['key'] = this.key;
    data['only1'] = this.only1;
    return data;
  }
}

class QuickSearch {
  List<String>? quickSearchColumn;

  QuickSearch({this.quickSearchColumn});

  QuickSearch.fromJson(Map<String, dynamic> json) {
    quickSearchColumn = json['quick_search_column'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['quick_search_column'] = this.quickSearchColumn;
    return data;
  }
}

class ListNode {
  String? id;
  String? madKey;
  String? isCustom;
  String? isLocked;
  String? isInactive;
  RunHyperlink? runHyperlink;
  List<Field>? field;

  ListNode(
      {this.id,
      this.madKey,
      this.isCustom,
      this.isLocked,
      this.isInactive,
      this.runHyperlink,
      this.field});

  ListNode.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    madKey = json['mad_key'];
    isCustom = json['is_custom'];
    isLocked = json['is_locked'];
    isInactive = json['is_inactive'];
    runHyperlink = json['run_hyperlink'] != null
        ? new RunHyperlink.fromJson(json['run_hyperlink'])
        : null;
    if (json['field'] != null) {
      field = <Field>[];
      json['field'].forEach((v) {
        field!.add(new Field.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['mad_key'] = this.madKey;
    data['is_custom'] = this.isCustom;
    data['is_locked'] = this.isLocked;
    data['is_inactive'] = this.isInactive;
    if (this.runHyperlink != null) {
      data['run_hyperlink'] = this.runHyperlink!.toJson();
    }
    if (this.field != null) {
      data['field'] = this.field!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class RunHyperlink {
  String? classid;
  String? id;
  String? madKey;

  RunHyperlink({this.classid, this.id, this.madKey});

  RunHyperlink.fromJson(Map<String, dynamic> json) {
    classid = json['classid'];
    id = json['id'];
    madKey = json['mad_key'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['classid'] = this.classid;
    data['id'] = this.id;
    data['mad_key'] = this.madKey;
    return data;
  }
}

class Field {
  String? fieldName;
  String? colIndex;
  String? fieldValue;
  String? madKey;
  String? id;

  Field({this.fieldName, this.colIndex, this.fieldValue, this.madKey, this.id});

  Field.fromJson(Map<String, dynamic> json) {
    fieldName = json['field_name'];
    colIndex = json['col_index'];
    fieldValue = json['field_value'];
    madKey = json['mad_key'];
    id = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['field_name'] = this.fieldName;
    data['col_index'] = this.colIndex;
    data['field_value'] = this.fieldValue;
    data['mad_key'] = this.madKey;
    data['id'] = this.id;
    return data;
  }
}

now create instance of this class and get value from api response

ResponseModel responseModel = ResponseModel.fromjson(jsonEncode(response.body));

and now you can access which ever property you want using responseModel

  • Related