remove quotes from json_encode keys

2019-08-29 10:34发布

问题:

I am new to jquery and I am trying to use Jquery UI to create an auto-complete.I'm still learning so I'm sure my code could be a lot better but this is just a start. I am having a problem where jquery is putting quotes around the json keys.I've had a look though stackoverflow and I can't seem to find a solution so I thought it's worth asking as I am well and truly stuck.I think it must be something wrong with my php somewhere.

{"value":"Managerial Accountants","id":"5929"}

I want my output to come out like this"

{value:"Managerial Accountants",id:"5929"}

This is the rest of my code:

 <script>
 jQuery(function(){
 jQuery(function () {
   var checkboxval;
   var availableTags = [
 <?php
// Database Connection
 error_reporting(-1);
 ini_set('display_errors', 'On');

 $con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

 $q="SELECT name as value,term_id as id FROM 7terms WHERE term_id not in 
     (select   parent from 7term_taxonomy) and term_id in (select term_id from   
     7term_taxonomy where  taxonomy='cat')";

$r = mysqli_query($con, $q);            

$city_state = array();
while($row = mysqli_fetch_assoc($r)){


 $rows[]=$row;
}
$json = json_encode($rows,true);
echo $json;
 ?>

];

//set autocomplete search
set_autocomplete_search("tags");
set_autocomplete_search("tags1");
set_autocomplete_search("tags2");

function set_autocomplete_search(p_tags) {
    var temp_p_tags = "#" + p_tags;
    jQuery(temp_p_tags).autocomplete({
        source: availableTags,
        select: function (event, ui) {
            var txtbx1 = (ui.item.name);
            var catid = (ui.item.id);
            alert(catid);
            jQuery(temp_p_tags).val(txtbx1);
            var tags = jQuery(temp_p_tags).val(txtbx1);
            //var checkboxval = "";
            checkboxval = tags.val();




            jQuery("#" + checkboxval + "").prop("checked", true);



        },
        change: function () {
            //alert("changed detected");
            //$("#" + checkboxval + "").prop("checked", false);
        }
    }).blur(function(event) {
            event.stopPropagation();
            event.preventDefault();

            //uncheck all checkboxes first
            jQuery(".test input:checked").each(function() {
                jQuery(this).attr("checked", false);
            });

            //set checkbox
            var checkbox_tags = jQuery("#tags").val();
            jQuery("#" + checkbox_tags + "").prop("checked", true);

            var checkbox_tags1 = jQuery("#tags1").val();
            jQuery("#" + checkbox_tags1 + "").prop("checked", true);

            var checkbox_tags2 = jQuery("#tags2").val();
            jQuery("#" + checkbox_tags2).prop("checked", true);
        });
   }
 });
});//]]>  

Thanks for your time.I really appreciate any help I can get with this as I still have a lot to learn and have been stuck on this for a couple of days.

回答1:

You are experiencing this issue because you are not using json correctly.

You can still just echo the json_encode() into the JS variable but get rid of the [] brackets and then use .parseJSON() to get what you need:

var availableTags = $.parseJSON('<?php
    // Database Connection
    error_reporting(-1);
    ini_set('display_errors', 'On');

    $con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

    $q="SELECT name as value,term_id as id FROM 7terms WHERE term_id not in 
         (select   parent from 7term_taxonomy) and term_id in (select term_id from   
         7term_taxonomy where  taxonomy='cat')";

    $r = mysqli_query($con, $q);            

    $city_state = array();
    while($row = mysqli_fetch_assoc($r))
    {
        $rows[]=$row;
    }
    echo json_encode($rows,true);
?>');

This solution should get you going but I would recommend opting for an AJAX based solution where your query is in a PHP file and call upon it with AJAX because $.ajax() can auto-convert a JSON string into the properly formatted array/object expected as long as you set dataType: 'json'