1

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

2
  • 1
    I'd just like to clarify that JSON is JSON, no matter where is was encoded. Commented Aug 22, 2011 at 23:47
  • possible duplicate of JSON in JQuery Commented Dec 26, 2011 at 7:11

2 Answers 2

3

$.getJSON() is what you want.

$.getJSON('path/to/file.json', function(response) {
   $('div#something').text(response[0].file);
});
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.