1

My code so far:

/apis/get_json.php returns:

{"1":"value1","2":"value2","3":"value3"}

as an array to json in PHP (json_encode).

<script>
$(document).ready(function(){
    $.get("/apis/get_json.php",function(json_response,status){
         var json_response=JSON.parse(json_response)
          alert("Data: " + json_response.[1] + "\nStatus: " + status);
});
});
</script>

I get the error : Uncaught SyntaxError: Unexpected token [

Not sure what the syntax is if the key is numeric

3
  • 3
    This is a good question in some ways: your code is decent, your testcase is minimal and complete, your inputs are clear and your error is clear. Unfortunately, it's not clear to me why you did not simply look up this language feature in a JavaScript reference, which would have revealed the problem to you exceedingly quickly. Commented Mar 28, 2014 at 1:40
  • I was not clear on why numbers required [] when variables are accessed with .varname so maybe I need to further review the differences between data types. This was helpful none the less. Commented Mar 28, 2014 at 1:55
  • It's just a matter of what qualifies as a valid property identifier. An identifier must begin with a letter, $ or _, followed by any of those or a number. A number can't be first, probably to avoid an ambiguity that the parser must then deal with since .9 would look like either a valid decimal number or a valid property. Using square brackets, any value can be used, though its toString evaluation is what is ultimately used. Commented Mar 28, 2014 at 2:10

1 Answer 1

4

Issue : To access the objects properties you can either use obj.prop or obj[prop].In your case you used both (.[]) which is syntax error so,

Convert,

alert("Data: " + json_response.[1] + "\nStatus: " + status); to

alert("Data: " + json_response['1'] + "\nStatus: " + status); // [`1`] just to avoid of implicit converstion 

Read More about Property Accessors

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

10 Comments

I'd rather use json_response['1'] without implicit conversion and to avoid confusion with a vanilla array. Object.keys(json_response) returns key names as strings, after all
@o.v.: There's no distinction between the indices of a vanilla array and the properties of any other object. They're all treated as strings.
"just to avoid headache of implicit converstion" What headache?
@cookiemonster I am talking about javascript engine coercion in case of [1]
Here's an updated test that includes an Array too. jsperf.com/propaccessor/2 It shows equal performance in Firefox, and much faster performance in Chrome when using [1] instead of ["1"]. Either way, doesn't matter. In Chrome, the speeds were nearly 1 billion operations per second. If/when there happens to be a performance difference on different browsers, it's not a compelling reason to alter coding style, since the difference is imperceptible and can change with the next release.
|

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.