Im using the Blueimp Jquery File Upload widget and its returning something that looks like an array of json data. If you look at the below example (not all json data to make it easier to read):
[{"name":"10 (2).jpg","size":264843,"type":"image\/jpeg"}]
Now, if I want to access the 'name' value of said array, how would I do it? Using the below, gets this done, but then my log gets full of 'undefined' values as well when the array is only a single element long:
$.each(data.result, function (index, file) {
console.log(file.name);
});
Doing console.log(data.result) spits the above array example i give, while trying to do console.log(data.result[0]) just spits out
[
Clearly Im not getting something. $.each method works fine with multiple elements, but why is it failing with a single item? Furthermore; and MORE IMPORTANTLY, how do i get the "name" value since in this current application thats all the info I reall need.
-
1Try replacing from $.each(data.result) to $.each(data)Murali Murugesan– Murali Murugesan2012年10月23日 12:12:14 +00:00Commented Oct 23, 2012 at 12:12
-
No, its definitely in the 'result'; just data doesnt work.SupaMonkey– SupaMonkey2012年10月23日 14:00:29 +00:00Commented Oct 23, 2012 at 14:00
2 Answers 2
You have a string, so data.result[0] will give the first char. It seems you have to parse the string:
var data = jQuery.parseJSON( data.result )
This might work.
2 Comments
Ok, so I added the "BlueImp" widget option to include:
dataType: 'json',
And suddenly console.log(file.name); works. Silly because thats the DEFAULT data type! Dont know why it starts working with the above, frankly dont care - thanks for the help guys.