Reputation: 5802
Json Array as following is easy to parse,
{
"movieCategories": [
"a" : "Animation",
"b" :"Romance",
"c" :"Science Fiction"
"d" : "Western"
]
}
Now,I have a Json response as following which is stored in movies.json file.
{
"movieCategories": [
"Animation",
"Romance",
"Science Fiction",
"Western"
]
}
I am not sure how to parse the above json array. Kindly help.
Thank you.
Upvotes: 2
Views: 31149
Reputation: 4294
you can only use the eval like
<html>
<div>
<script language="javascript">
var arr = '[{ "movieCategories": [ "Animation", "Romance", "Science Fiction" ,"Western" ] }]';
var b = eval(arr)[0];
alert(b.movieCategories);
</script>
</div>
</html>
Upvotes: 1
Reputation: 3721
Download json2.js from here https://github.com/douglascrockford/JSON-js Include it on your page. Use like this
jsonobj = JSON.parse(jsonstring);
jsonstring = JSON.stringify(jsonobj);
Upvotes: 3
Reputation: 307
Assume
var test = {
"movieCategories": [
"Animation",
"Romance",
"Science Fiction"
"Western"
]
}
then
test.movieCategories[0] // Will be Animation
Upvotes: 7
Reputation: 23563
According to JSLint this is invalid JSON. There is a "," missing:
{ "movieCategories": [ "Animation", "Romance", "Science Fiction", "Western" ] }
For the rest see: Is it possible to parse JSON with javascript?
Upvotes: 1