0

Ok, So I'm using AJAX to pull some information from my MYSQL database and put it onto the screen. The thing is I cannot understand JSON for the life of me. Can you suggest any tutorials or anything that will help?

I mean I get that I can encode my query via JSON but I guess it's the javascript side I do not understand.

0

5 Answers 5

1

Here's my quick tutorial:

JSON is a means for expressing data for arrays, objects, and their contents. It has nothing to do with object behaviour (methods).

<?php
class Test {
    public $hello = 'hello';
    public $something = array('hello1', 'hello2');

    public __construct() {

    }

    public void printHello() {
        echo $this->hello;
    }
}
?>

This class would in JSON would look like:

var obj = {
   "hello": "hello",
   "something": ["hello1", "hello2"] 
};

As you can see, JSON is similar to maps in a lot of languages (key/value pairs). You can also see, that only data is represented. JSON is also shorthand for JavaScript builtins. For example, this previous object can be written in JavaScript like so.

   var obj = new Object();
   obj.hello = "hello";
   obj.something = new Array("hello1", "hello2");

Hope this gives you a little idea of what JSON is about.

Sign up to request clarification or add additional context in comments.

3 Comments

But how would you pull those values once their in JSON? print(obj[0]); ?
I'm not following. Are you asking how to get those values back into php objects?
oh no sorry, not pull. I mean print the values to HTML or anything.
1

First, read this: http://www.json.org/js.html

Then, practice with this: http://jsonlint.com/

Comments

0

You can read about using JSON in JavaScript at the Mozilla docs.

Comments

0

Maybe this short example will help you: http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php.

jQuery docs - getJSON()

Comments

0

How to use JSON (updated with example)

http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/

1 Comment

Thanks but that's the JQuery version, i barely understand AJAX(JQuery) as it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.