Access values ​​from next JSON

How to access the second number "19", which is in the Numbers array in the next JSON? I tried every way and could not.

{ "Numbers": [{ "1": 6 }, { "2": 19 }, { "3": 34 }, { "4": 38 }, { "5": 70 }], "MB": 5, "MP": "05", "DrawDate": "2016-03-22T00:00:00" } 
+5
source share
6 answers

You would access it as follows:

 console.log(jsonObj.Numbers[1][2]); 

This assumes that you are storing this JSON in a variable called jsonObj . You cannot use numbers as the property key of an object, so you cannot just do jsonObj.Numbers[1].2 .

+3
source

Access to:

 object.Numbers[1]['2'] 

This is because the Numbers object is an array of objects with the key in which you want.

+4
source

You can access it using

 obj.Numbers[1][2] 

where 1 is the index of the object in the array, and 2 is its key

+1
source

To do this, you must parse it in Javascript, for example:

 var myjson = JSON.parse('{"Numbers":[{"1":6},{"2":19},{"3":34},{"4":38},{"5":70}],"MB":5,"MP":"05","DrawDate":"2016-03-22T00:00:00"}'); 

After analyzing it, you can do

 myjson.Numbers[1][2] 

To access the key ( 2 : 19), you would do

 myjson.Numbers[1] 
+1
source

Assign all json to var variable.

 var obj = JSON.parse({"Numbers":[{"1":6},{"2":19},{"3":34},{"4":38},{"5":70}],"MB":5,"MP":"05","DrawDate":"2016-03-22T00:00:00"}); obj.Numbers[1][2] 

You must have access to it

+1
source

I think this is very easy:

 var myVars={"Numbers":[{"1":6},{"2":19},{"3":34},{"4":38},{"5":70}],"MB":5,"MP":"05","DrawDate":"2016-03-22T00:00:00"} 

myVars.Numbers will give you: [{"1":6},{"2":19},{"3":34},{"4":38},{"5":70}] This element represents an array: the first element is myVars.Numbers[0] - {"1":6} , the second is myVars.Numbers[1] - {"2":19} . Finaly, myVars.Numbers[1][2] is 19. This means that if you want to access the second number 19 , you need to write: myVars.Numbers[1][2]

+1
source

Source: https://habr.com/ru/post/1245747/


All Articles