Home > Enterprise >  How to get only json values from a json list without the labels or tags
How to get only json values from a json list without the labels or tags

Time:08-17

I have a list of data coming from MySQL database and I want to show them in flutter with a dropdown button. It worked, only the fact that it is displaying the list item in json format like the image below. How do I make it show just the values alone without the curly braces and label? The Image Screenshot

The code fetching the data from MySQL database is:

<?php require_once("connect.php") ?>

<?php

$res = $con->query("SELECT businessName FROM sellertable");

$list = array();

if($res){
    while($row = mysqli_fetch_assoc($res)){
        $list[] = $row;
    }

    echo json_encode($list, JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT); 
}

?>

and the flutter code plus dropdown button code looks like this:

//Retrieving Business Company Name From Database

late String onlythecompany = thecompany; //url to the php script above

  var onlyCompany = [];

  justTheCompany() async {
    var response = await http.get(Uri.parse(onlythecompany));
    if (response.statusCode == 200) {
      setState(() {
        onlyCompany = json.decode(response.body);
      });
    }
    return onlyCompany;
  }

//body area with dropdown button

Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                          width: 1,
                          color: globalInfoColor,
                          style: BorderStyle.solid),
                    ),
                  ),
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton(
                      value: onlyCompany[0]
                          .toString(),
                      items: onlyCompany.map((ditem) {
                        return DropdownMenuItem(
                          child: Text(
                            ditem.toString()
                          ),
                          value: ditem.toString(),
                        );
                      }).toList(),
                      onChanged: (String? dValue1) {
                        setState(() {
                          onlyCompany[0] = dValue1!;
                        });
                      },
                    ),
                  ),
                ),

Any idea how to get the business names, e.g. Chick's jus find JFK's, Festova Farms, etc. without the curly braces and the label 'businessName': ?

CodePudding user response:

Either use mysqli_fetch_column:

if($res){
    while($row = mysqli_fetch_column($res)){
        $list[] = $row;
    }

    echo json_encode($list, JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT); 
}

OR use array_column:

if($res){
    while($row = mysqli_fetch_assoc($res)){
        $list[] = $row;
    }

    $list = array_column($list, 'businessName');

    echo json_encode($list, JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT); 
}
  • Related