Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access the values form the following JSON

How do I access the second number "19", which is in the Numbers array in the following JSON? I've tried every which way and have not been able to.

{
  "Numbers": [{
    "1": 6
  }, {
    "2": 19
  }, {
    "3": 34
  }, {
    "4": 38
  }, {
    "5": 70
  }],
  "MB": 5,
  "MP": "05",
  "DrawDate": "2016-03-22T00:00:00"
}
like image 282
ConfusedDeer Avatar asked Mar 25 '16 04:03

ConfusedDeer


People also ask

How can we access values from JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How do I access a JSON file?

JSON files are human-readable means the user can read them easily. These files can be opened in any simple text editor like Notepad, which is easy to use. Almost every programming language supports JSON format because they have libraries and functions to read/write JSON structures.

What is JSON how you will access a JSON?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

What are keys and values in a JSON object?

Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma. Values in a JSON object can be another JSON object.

How to access a property of a JSON object?

To access a property of your JSON do following: data[0].name; data[0].address; Why you need data[0] because data is an array, so to its content retrieve you need data[0] (first element), which gives you an object {"name":"myName" ,"address": "myAddress" }. And to access property of an object rule is:

How do you write a name and value in JSON?

JSON Data - A Name and a Value. JSON data is written as name/value pairs. A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value: "name":"John". JSON names require double quotes.

What are the data types in JSON?

JSON Values. In JSON, values must be one of the following data types: a string. a number. an object (JSON object) an array. a boolean. null.


2 Answers

You can access with:

object.Numbers[1]['2']

That is because the Numbers object is an array of key-value objects in which is your desired value.

like image 127
LuisFMorales Avatar answered Oct 12 '22 16:10

LuisFMorales


You would access it like this:

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

This assumes that you store that JSON into a variable called jsonObj. You cannot use numbers as object property key's so you cant just do jsonObj.Numbers[1].2.

like image 27
Kyle Avatar answered Oct 12 '22 17:10

Kyle