简体   繁体   English

在客户端将PHP转换为JSON

[英]PHP to JSON on client side

I'm using Rob Monies 'Jquery Week Calendar' to build a calendar application. 我正在使用Rob Monies'Jquery Week Calendar'构建日历应用程序。

I have a MSSQL DB a with table named 'dates' and the following fields: 我有一个名为表“ dates”和以下字段的MSSQL DB:

id start end title id开始结束标题

I would like to query this DB in PHP and then pass the results to Javascript, Javascript will then render the events in the browser. 我想用PHP查询该数据库,然后将结果传递给Javascript,然后Javascript将在浏览器中呈现事件。

The Javascript needs to receive the event details in JSON format as per the following example: Javascript需要按照以下示例以JSON格式接收事件详细信息:

{"id":1,"start": 2010-10-09T13:00:00,"end":2010-10-09T14:00:00,"title":"Lunch with Mike"},
{"id":2,"start": 2010-10-10T13:00:00,"end": 2010-10-10T14:00:00, "title":"Dev Meeting"}

So far, to keep things simple I've just returned one row from the DB at a time, however - I will need the application to be able to render multiple events, as stored in the database. 到目前为止,为简单起见,我一次只从数据库返回了一行,但是-我将需要应用程序能够呈现存储在数据库中的多个事件。

I've tried using json_encode() to put the values into a var and pass them to a Javascript var called DB_events - if I return the DB_events in an alert box on the client side I see the following; 我尝试使用json_encode()将值放入var并将它们传递给名为DB_events的Javascript var-如果我在客户端的警报框中返回DB_events,则会看到以下内容;

{"id":1, "start":2010-10-09T13:00:00, "end":2010-10-09T14:00:00,"title":"Lunch with Mike"}

which looks ok, but when I do this in my code: 看起来不错,但是当我在代码中这样做时:

events : [DB_events]

It doesn't work :( 它不起作用:(

If I take PHP out of the equation, and just do the following on the client side: 如果我不考虑PHP,只需在客户端执行以下操作:

var DB_events = {"id":1, "start":2010-10-09T13:00:00, "end":2010-10-09T14:00:00,"title":"Lunch with Mike"};

and return DB_events in an alert box, I get: 并在警报框中返回DB_events,我得到:

[object] [Object]

But when I do this: 但是当我这样做时:

events : [DB_events]

it works! 有用!

Back to PHP … 回到PHP…

If I put the SQL result into PHP vars as follows: 如果将SQL结果放入PHP变量中,如下所示:

$id = id;
$start = start;
$end = end;
$title = title;

and pass those vars to the following JS vars: 并将这些变量传递给以下JS变量:

JS_id
JS_start
JS_end
JS_title

and do this on the client side: 并在客户端执行此操作:

var DB_events = {"id":JS_id, "start":JS_start, "end":JS_end,"title":JS_title};

events : [DB_events]

that also works. 这也有效。

As you can probably tell - I'm new to this and probably missing something very basic. 您可能会说-我是新手,可能会错过一些非常基本的东西。

Any help, advice or information would be very much appreciated :) 任何帮助,建议或信息将不胜感激:)

Many Thanks 非常感谢

Tim 蒂姆

  1. when you get a string representation of your object it means that you've exported it as a string not as an object. 当您获得对象的字符串表示形式时,表示您已将其导出为字符串而不是对象。

  2. when you get [object] [Object] it means that it is now an object, which is right! 当您获得[object] [Object]它意味着它现在是一个对象,这是正确的!

You can do this with JSONP if you get JSON from an URL : 如果您从URL获得JSON,则可以使用JSONP进行此操作:

// send the request via <script> tag
var src    = "..."; // url of the JSON output
var head   = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", src); 
head.appendChild(script);

Or parse it as a string with JSON parser : https://github.com/douglascrockford/JSON-js/blob/master/json2.js 或使用JSON解析器将其解析为字符串https : //github.com/douglascrockford/JSON-js/blob/master/json2.js

var DB_events = JSON.parse( ...JSON output as a string... );

Or by directly passing it from PHP with an inline <script> block in your page: 或通过在页面中使用内联<script>从PHP直接传递它:

echo "<script>";
echo "var DB_events = " . json_encode( ... ) . ";";
echo "</script>";

Did you use jsocn decode 您是否使用过jsocn解码

http://php.net/manual/en/function.json-decode.php http://php.net/manual/en/function.json-decode.php

You can split the result and make it differenct variables like start ,end , events etc 您可以拆分结果并使其具有不同的变量,例如start,end,events等

http://php.net/manual/en/function.explode.php http://php.net/manual/en/function.explode.php

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

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