douliang1891 2014-01-26 22:04
浏览 94
已采纳

从json_encode键中删除引号

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条回答 默认 最新

  • dongqi8030 2014-01-26 23:10
    关注

    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'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!