简体   繁体   English

如何将php数组传递给jquery并转换为json?

[英]How to pass php array to jquery and turn into json?

Using php4 how could I store an array in a hidden div so that jquery can pick it up and then turn it into a json object? 使用php4,如何将数组存储在隐藏的div中,以便jquery可以将其拾取,然后将其转换为json对象? Im trying to pass an array of data to gmap so that I can load multiple markers, but cant seem to figure out how to pass a php array to jquery and then turn it into a json object. 我试图将数据数组传递给gmap,以便我可以加载多个标记,但似乎无法弄清楚如何将php数组传递给jquery,然后将其转换为json对象。

I tried using a custom json encode class for php4 and encoding the php array to json, the problem is it spits out a bunch of garbage characters and appears to cause errors in the jquery code. 我尝试为php4使用自定义json编码类,并将php数组编码为json,问题是它会吐出一堆垃圾字符,并似乎在jquery代码中引起错误。 Here is the array Im creating in php: 这是我在php中创建的数组:

$map_array[] = array('latitude' => $result_latitude,'longitude' => $result_longitude,'html' => $result_html,'title' => $result_name,'icon' => array('image' => '/pathtoicon' .$mapi .'.png','iconsize' => array(27,27)));

$map_json = $json->encode($map_array);

Then in jquery: 然后在jQuery中:

    var mapcoords = $('#mapcoord').html();

        $('#rmap').gMap(
                {
                    zoom: 10,
                    markers:$.parseJSON(mapcoords)
                }

        );

When I do this, I get this error: 当我这样做时,出现以下错误:

"SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data" “ SyntaxError:JSON.parse:JSON数据后出现意外的非空白字符”

This works if I do var mapcoords = $('#mapcoord').text(); 如果我做var mapcoords = $('#mapcoord')。text();这可以工作

The problem is, it strips out the html. 问题是,它删除了html。 So how do I pass the php array to jquery and be able to preserve the html code? 那么,如何将php数组传递给jquery并能够保留html代码?

Why don't you simply create the javascript variable with php and set it there? 您为什么不简单地用php创建javascript变量并将其设置在那里?

For example: 例如:

<?php

   $obj = "{'test': 1}";

    echo "<script type='text/javascript'>";
    echo "var myString = " . $obj;
    echo "</script>";
?>

Now the variable is accessible through javascript as an object. 现在可以通过javascript作为对象访问该变量。

If you can't do that, you can try to convert the string into an json object using jQuery.parseJSON . 如果无法做到这一点,则可以尝试使用jQuery.parseJSON将字符串转换为json对象。

Hope it helps! 希望能帮助到你!

Did you try jQuery.parseJSON(myString) ? 您是否尝试过jQuery.parseJSON(myString)

Assuming that the html page and the json data are sent from the webserver to the client in the same request you should write the necessary js in the server side as php strings. 假设html页面和json数据是在同一请求中从Web服务器发送到客户端的,则应在服务器端以php字符串形式编写必要的js。 The following snippet should illustrate what needs to be done: 以下代码段应说明需要做什么:

$map_json = "var jsonData = " . $json->encode($map_array); 
$map_json = '<script type="text/javascript">' . $map_json . '</script>'; 
echo $map_json;

The variable jsonData should be accessible for your js snippet in the client. 客户端中的js代码段应该可以访问变量jsonData。

I would try something like this: 我会尝试这样的事情:

$json = json_encode($my_array);
echo <<<EOS
<script type="text/javascript">
     my_global_js_var = $json
</script>
EOS;

Since JSON is valid javascript, you should get a variable with the json content. 由于JSON是有效的javascript,因此您应该获取带有json内容的变量。 You even do not need an hidden HTML Element. 您甚至不需要隐藏的HTML元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM