Reputation: 123
I have JSON value like this:
{"223":[{"virtuemart_state_id":"1","state_name":"Alabama"},{"virtuemart_state_id":"2","state_name":"Alaska"}]}
& I am trying to parsing data like this:
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
var state,
url = 'http://localhost/jquery/test.json';
$.getJSON(url, function(data){
console.log(data);
$.each(data.223, function(i, rep){
state += "<option value = '" + rep.virtuemart_state_id + "'>" + rep.state_name + "</option>";
});
$("#state").html(state);
});
});
</script>
</head>
<div id="result">
<select id="state">
</select>
</div>
But it's not working with the number 223 & I am getting error like this: SyntaxError: missing ) after argument list
Any idea in where I made mistake ? Thanks
Upvotes: 1
Views: 421
Reputation: 360872
data.223
is not valid Javascript. It'd have to be data['223']
. The .
shortcut notation is handy, but it can't handle all the possible key-names you actually create in JS.
Upvotes: 1
Reputation: 414006
You can't refer to an object property whose name isn't a valid identifier using the .
operator. Instead, you'd do this:
$.each(data[223], function(i, rep){
Upvotes: 0