PT Desu
PT Desu

Reputation: 693

how to pull values from PHP JSON with jQuery JSON?

Ok here is the thing I got PHP to encode some MySQL information into JSON format and then force jQuery to get this via a file located on my server. The PHP generated JSON encode looks like this, the problem is I do not understand how would I go about pulling file, size, name, preview from the JSON array with use of jQuery

Here is a sample JSON array

[{"aid":"416","type":"image\/jpeg","size":"1826611","file":"http:\/\/*****.com\/aws-eu\/-f\/Cat.jpg","user_id":"0","token":"b5bb0380c6912cd3464747b9f29355e7","name":"Cat.jpg","qp_tag":"1f3a4e30x","preview":"aws-eu\/-t\/sample_Cat.jpg","oath":"b5bb0380c6912cd3464747b9f29355e7"},

Could someone help me out with pulling information from it to jQuery and displaying it in a DIV afterwords

Upvotes: 1

Views: 208

Answers (2)

Paul
Paul

Reputation: 141829

You can specify a callback to getJSON so that you can read the JSON's data after it's been retrieved from the PHP file's URL. The JSON data is returned as the parameter to the callback:

$.getJSON('yourphpfile.php', function(data){
    // Here data[0].size === "1826611"
});

Since your JSON data is seems to be an array, you can access the first items of the array by doing data[0]. You can access any property of that item by using its name, e.g. data[0].size.

Upvotes: 3

alex
alex

Reputation: 490143

$.getJSON() is what you want.

$.getJSON('path/to/file.json', function(response) {
   $('div#something').text(response[0].file);
});

Upvotes: 3

Related Questions