简体   繁体   English

从Javascript获取JSON数据

[英]Getting JSON data from Javascript

I am currently working on a php/javascript project which retrieves information from a database and json encodes the data. 我目前正在研究一个php / javascript项目,该项目从数据库中检索信息,并且json对数据进行编码。 It is supposed to show the values from the database inside a combo box on the web page. 应该在网页上的组合框中显示数据库中的值。

In the PHP script that encodes the mysql data I have the following code: 在对mysql数据进行编码的PHP脚本中,我具有以下代码:

$query = "SELECT pla_platformName as `platform` FROM platforms";

    $result = mysql_query($query);

    if ($result)
    {       
        while ($myrow = mysql_fetch_array($result))
        {
            $output[] = $myrow;                     
        }

        print json_encode($output);
        die();
    }

In the javascript code I have the following: 在javascript代码中,我具有以下内容:

<script>
        $(document).ready(function()
        {
            getPlatforms();
            function getPlatforms()
            {
                $.post("phpHandler/get-platforms.php", function(json)
                    {
                        alert(json);    
                        alert(json.platform);
                    }
                );
            }
        });
    </script>

I have alert(json); 我有alert(json); which shows the entire json data which looks like the following: 显示整个json数据,如下所示:

[{"0":"hello","platform":"hello"},{"0":"android","platform":"world"}] [{“ 0”:“你好”,“平台”:“你好”},{“ 0”:“ android”,“平台”:“世界”}]

The next alert(json.platform) I am expecting it to show either hello or world or both but on this line it keeps on saying undefined. 我期望下一个alert(json.platform)显示问候或世界,或同时显示两者,但在这一行中,它会继续显示未定义。 Why isn't this working and how do I get a specific platform, ie either hello, or world. 为什么这样不起作用,以及如何获得特定的平台,即“ hello”或“ world”。

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

Your platform member is defined on each item, you'll have to specify which array item you want the platform for. 您的平台成员是在每个项目上定义的,您必须指定平台要用于哪个数组项目。 This should do the trick: 这应该可以解决问题:

alert(json[0].platform);

I'm assuming that your json parameter actually holds a javascript object, and not simply a string. 我假设您的json参数实际上包含一个javascript对象,而不仅仅是一个字符串。 If it is a string, you should define contentType 'application/json' on your php page. 如果是字符串,则应在php页面上定义contentType'application / json'。 (I'm not sure how you do that in php since I do .NET for server side myself.) (我不确定您是如何在php中执行此操作的,因为我自己是在服务器端执行.NET的。)

To test it quickly however, you can do a $.parseJSON(json) to get the object. 为了快速测试它,您可以执行$ .parseJSON(json)来获取对象。

You need to first convert your JSON string into an object 您需要先将JSON字符串转换为对象

var data = $.parseJSON(json);

In this case, the object returned is an array. 在这种情况下,返回的对象是一个数组。 Try 尝试

alert(data[0].platform);

You can skip the first step if you set the dataType option to json in your ajax call. 如果在ajax调用中将dataType选项设置为json ,则可以跳过第一步。

$.post("phpHandler/get-platforms.php", function(data) {
     alert(data[0].platform);
  }, 
  'json'
 );

See jQuery.post() documentation 参见jQuery.post()文档

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

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