从json_encode键中删除引号 [英] remove quotes from json_encode keys

查看:87
本文介绍了从json_encode键中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是jquery的新手,我正在尝试使用Jquery UI创建自动完成功能.我仍在学习,因此可以肯定我的代码会更好,但这只是一个开始. 我遇到一个问题,即jquery在json键周围加上引号.我虽然看了stackoverflow,但似乎找不到解决方案,所以我认为值得一问,因为我身体健康且确实卡住了.我的php一定有问题.

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"}

这是我其余的代码:

 <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.

推荐答案

由于未正确使用json,因此遇到此问题.

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

您仍然可以只将json_encode()回显到JS变量中,但是去掉[]括号,然后使用.parseJSON()即可获得所需的内容:

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);
?>');

该解决方案可以助您一臂之力,但是我建议选择一种基于AJAX的解决方案,其中您的查询位于PHP文件中,并使用AJAX对其进行调用,因为

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'

这篇关于从json_encode键中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆