我在哪里可以通过PHP找到关于JSON的好视频或教程?

sam*_*sam 5 php jquery json

我已经多次搜索网络,很多JSON教程太难理解了.我正在寻找使用jQuery和PHP的JSON.如果有人知道我可以看到任何视频或网站,这将是伟大的

谢谢

Har*_*men 10

什么是JSON以及如何创建JSON对象?

你唯一应该知道的是JSON实际上是Javascript代码.您可以使用字符串数字(以及数组和对象)等值创建数组对象

  • 您可以在数组中存储大量值,并用逗号分隔,如下所示:

    [12, "string", -30] // This is an array with 3 values

  • 您可以使用自己的密钥存储在对象中用逗号分隔的大量值,如下所示:

    {"key1": "value1", "key2": 234} // This is an object with two pair of keys and values

有趣的是,您可以将数组和对象用作值.因此,当您将上面学到的所有内容放在一起时,您可以获得如下的JSON代码:

{                  // Creates an object
    "key1": 12,    // with a key called "key1" and a number as value
    "key2": [      // and another key called "key2", with an new array as value
        10,        // the array has the number ten as first value
        "value"    // and a string containing "value" as its second value
    ]
}
Run Code Online (Sandbox Code Playgroud)

我该如何使用JSON?

您可以使用JSON作为在服务器和客户端之间传输数据的简单方法,例如Twitter API.

在客户端,您可以通过Javascript和AJAX发送和接收JSON数据.通常人们使用jQuery作为这个库,因为它有一些JSON验证的东西.在服务器端,您可以使用PHP作业将JSON数据转换为PHP对象.

您可以在Javascript中以这种方式访问​​值:

someArray[0]; // gives you access to the first value of an array
someObject.key; // gives you access to the value of an object with key 'key'
Run Code Online (Sandbox Code Playgroud)

让我举个例子,你将打开flickr流:

// $.getJSON(url, dataHandlerFunction); to get JSON-data
// Add the first image of the Flickr stream to the page
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", 
  function(data){

    // In this function you can do anything with the JSON code, because it's transformed into a Javascript object
    // If you open the above URL in your browser, you see that it exists of and object with
    // a key called items. Within is an array of objects, representing images
    // let's add the first one to our page
    var firstImg = data.items[0]; // First object, with image data.

    $("<img/>").attr("src", firstImg.media.m).appendTo('body');
  }
);
Run Code Online (Sandbox Code Playgroud)

在PHP中你几乎可以做同样的事情:

// Get the JSON code form Flickr
$contents = file_get_contents('http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?'); 

// Decode it to a PHP object
$flickrStream = json_decode($contents);

// Display the image
echo '<img src="' . $flickrStream->items[0]->media->m . '" />';
Run Code Online (Sandbox Code Playgroud)

示例:在客户端和服务器之间传输数据

正如我所说,您可以使用AJAX在服务器和客户端之间发送数据.让我举几个简单的例子......你向服务器发送一些东西,服务器发回一些东西.

// Sends a GET request to the server
$.ajax({
  url: '/to/your/php/file/',
  dataType: 'json',
  data: {
    "name": "client"
  },
  success: function(data){
    alert(data.messageFromServer); // alerts "Hi back, 
  }
});
Run Code Online (Sandbox Code Playgroud)

PHP文件(这种方式非常不安全,但这是一个简单的例子)

<?php
  // A GET request was send, so you can use $_GET
  echo '{
          "messageFromServer": "Hi back, ' . $_GET['name'] . '"
        }';
?>
Run Code Online (Sandbox Code Playgroud)


tam*_*asd 1

您所需要的只是了解 JSON 是不同格式的数据。您可以轻松地将 PHP 数据结构转换为 JSON (json_encode()),并解析 JSON 字符串 (json_decode())。

检查 JSON 的 PHP 文档(上面链接)和 $.ajax() 的 jQuery 文档。

好的,有一个例子。

JavaScript:

$.ajax({
  url: 'json.php',
  success: function(data) {
    var ul = $('<ul></ul>');
    for(var i in data) {
      ul.append($('<li></li>')
          .append($('<strong></strong>').html(i))
          .append(': ')
          .append($('<span></span>').html(data[i]))
      );
    }
    $('body').append(ul);
  }
});
Run Code Online (Sandbox Code Playgroud)

PHP 文件:

<?php
$dbh = new PDO($connectionstring, $username, $password);
$response = array();
foreach($dbh->query('SELECT id, title FROM table ORDER BY id') as $record) {
  $response[$record['id']] = $record['title'];
}
print json_encode($response);
Run Code Online (Sandbox Code Playgroud)

当 jQuery 代码运行时,它会从 PHP 文件请求数据。PHP 文件获取一些内容并将其打印为 JSON 代码。响应返回,jQuery 将 JSON 解析为数据并触发回调函数。