Where can I find a good video or tutorial on JSON with PHP?

I searched the net many times and many JSON tutorials are too hard to understand. I am looking for JSON with jQuery and PHP. if anyone knows any videos or website I can see what would be great

thank

+5
source share
3 answers

What is JSON and how do I create JSON objects?

The only thing you should know is that JSON is actually Javascript code. You can create arrays and objects with values, such as strings and numbers (as well as arrays and objects)

  • , , :

    [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
    ]
}

JSON?

JSON , , API Twitter.

JSON Javascript AJAX. 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'

, 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 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 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');
  }
);

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 . '" />';

:

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, 
  }
});

PHP ( , )

<?php
  // A GET request was send, so you can use $_GET
  echo '{
          "messageFromServer": "Hi back, ' . $_GET['name'] . '"
        }';
?>
+10

, , , JSON - . PHP JSON (json_encode()) JSON (json_decode()).

PHP JSON (. ) jQuery $.ajax().

, .

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);
  }
});

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);

jQuery-, PHP. PHP JSON. , jQuery JSON .

+1

Not specifically json + php, but: Douglas Crockford - JSON Saga

+1
source

All Articles