从PHP JSON对象值 [英] JSON object value from PHP

查看:149
本文介绍了从PHP JSON对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中使用JSON,现在我需要从JavaScript访问它。我如何通过一个JSON对象的JavaScript?

I am using JSON in PHP, and now I need to access it from JavaScript. How do I pass a JSON object to JavaScript?

<?php
    $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");
    $json = json_encode($array);
>

在这里My.js有:

where My.js has:

showAll(){
    alert("Show All Json Objects");
    // How do I get the JSON value here?
}

我该怎么办呢?

How can I do it?

推荐答案

假设你正在使用Ajax作为你的方法下载JSON,你会呼应的json_en code的结果:

Assuming that you're using Ajax as your method to download the JSON, you would echo the result of the json_encode:

<?php
    $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");

    echo json_encode($array);
?>

然后您回电事件中,你会EVAL响应:

And then within your call back event, you'd eval the response:

var obj = eval('(' + req.ResponseText + ')');
for(var i in obj) {
    alert(i + ': ' + obj[i]);
}

假设你有一个 XMLHtt prequest 对象名称 REQ

这篇关于从PHP JSON对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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