Monday, January 29, 2018

How do you find the value of this JSON object --- {"70":27.4"} ?

These are two validated JSON objects:

I am trying to find the value for the second JSON object:
The code shown at the end works and provides a value of 2.85
----------------------
var jobj2={"tuesday":"2.85"};
alert(jobj2.tuesday );//---alerts 2.85

------------------------------------
If I use the exact code that I used for recovering the value for the first JSON object, I end up with an error.
---------------
var obj2={"70":"27.4"};
alert(obj2.70);//--spawns an error
--------------------
The work around which I guessed that would work for the first object provides the value 27.4

var x;
x=70;
var obj={"x":"27.4"};
alert(obj.x);----//alerts 27.4

---------------------------------------

If you want to take a look, just take out the relevant comments and run. There could be a better way of resolving this rather than re-formatting the question.


No comments: