Durga Dutt
Durga Dutt

Reputation: 4113

Json in javascript

i haven't used json before . When i am using eval function on json response than it gives me error of missing ']'. My json is as follows

[{"name":"154.jpg","size":620888,"type":"image\/jpeg","url":"\/active\/components\/com_mtree\/img\/listings\/o\/Tulips.jpg","thumbnail_url":"\/active\/components\/com_mtree\/img\/listings\/s\/Tulips.jpg","delete_url":"\/active\/components\/com_mtree\/img\/listings\/upload.php?file=Tulips.jpg","delete_type":"DELETE"}]

is this any error in this json. in javascript i am using this

var myObject = eval('(' + data + ')'); 

Upvotes: -1

Views: 178

Answers (2)

Mutation Person
Mutation Person

Reputation: 30530

Why are you prepending/appending brackets to it? As long as its a string, it works fine:

var x= '[{"name":"154.jpg","size":620888,"type":"image\/jpeg","url":"\/active\/components\/com_mtree\/img\/listings\/o\/Tulips.jpg","thumbnail_url":"\/active\/components\/com_mtree\/img\/listings\/s\/Tulips.jpg","delete_url":"\/active\/components\/com_mtree\/img\/listings\/upload.php?file=Tulips.jpg","delete_type":"DELETE"}]';

alert(eval(x));

See http://jsfiddle.net/gCE25/1/

This sort of thing is good if you are learning, however, I would echo @Quentin's sentiments - use a proper JSON Parser.

Upvotes: 0

Quentin
Quentin

Reputation: 944528

When i am using eval function

Don't do that. Use a real JSON parser.

it gives me error of missing ']'.

It shouldn't do, since the JSON is valid. You might be using it wrong, but you didn't show any code so it is hard to say. Using a real JSON parser is likely to help with that anyway.

Upvotes: 0

Related Questions