1

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this: $.each(data[223] . You can't refer object property with .

Comments

0

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){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.