JavaNinja's Profile
Ninja
100000380
Points

Questions
185

Answers
186

  • Ninja Asked on 19th September 2018 in JSON.

    JSON.stringify() converts a primitive value, object or array to a JSON-formatted string. A JSON stringify() works in the opposite direction of JSON.parse(), converting JavaScript data structures into JSON text.

    var obj = JSON.stringify([1, “false”, false]);
    //Produces “[1, “false”, false]”

    var jsObject = {firstname: “James”, lastname: “Jackson”, age: 25};

    var jsonObj = JSON.stringify(jsObject);
    //Produces “{“firstname”:”James”, “lastname”:”Jackson”, “age”: 25}”

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 1469 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 19th September 2018 in JSON.

    JSON objects can be created with JavaScript. Let us see various ways of creating JSON objects using JavaScript:

    Creation of an empty Object:
    var JSONObj = {};

    Creation of new Object:
    var JSONObj = new Object();

    Creation of an object with attribute employeename with value in string, attribute age with numeric value. Attributes is accessed by using ‘.’ operator:
    var JSONObj = { “employeename “:”James”, “age”:25 };

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 1463 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 19th September 2018 in JSON.

    Native JSON support is included in all major browsers and in the latest ECMAScript (JavaScript) standard:

    • Firefox (Mozilla) 3.5
    • Internet Explorer 8
    • Chrome
    • Opera 10
    • Safari 4

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 1217 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 19th September 2018 in JSON.

    JSON is derived from JavaScript and its syntax is mostly a subset of the language, it is often possible to use the JavaScript eval() function to parse JSON data. The eval() function can compile and execute any JavaScript program represented in the form of a string. This is considered unsafe because of potential security issues. Instead, a JSON parser library or JavaScript’s native JSON support like JSON.parse() should be used for reading and writing JSON. A correctly implemented JSON parser will only accept valid JSON, preventing potentially malicious code from being inadvertently executed. Performance wise also eval() is also generally slower than the JSON.parse() method.

    As mentioned above, eval will execute any valid JavaScript code. Thus the following would cause an alert:
    var text = “alert(‘Test eval and parse’)”;
    var obj = eval(text);

    JSON.parse, however, will only return successfully if the string passed in is valid JSON:
    // gives “SyntaxError: JSON.parse”
    var txt = JSON.parse(text);

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 4358 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 19th September 2018 in JSON.

    JSON object can be accessed in JavaScript using JSON.parse() method and the values can be accessed using “.” or “[]”. The JSON.parse() method parses a string as JSON.

    <script>
    var text = ‘{“friends”:[‘ +
    ‘{“firstName”:”Nitin”,”lastName”:”K L” },’ +
    ‘{“firstName”:”Nikhil”,”lastName”:”Dhankani” },’ +
    ‘{“firstName”:”Ananya”,”lastName”:”S A” }]}’;

    obj = JSON.parse(text);

    alert(obj.friends[0].firstName+” “+obj.friends[0].lastName); // Displays Nitin K L
    </script>

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 1489 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 19th September 2018 in JSON.

    JSON has the following syntax:

    • Objects are enclosed in braces ({}), their name-value pairs are separated by a comma (,), and the name and value in a pair are separated by a colon (:). Names in an object are strings, whereas values may be of any of the data types, including another object or an array.
    • Arrays are enclosed in brackets ([]), and their values are separated by a comma (,). Each value in an array may be of a different type, including another array or an object.
    • When objects and arrays contain other objects or arrays, the data has a tree-like structure.

    The following example shows JSON data for a sample object that contains name-value pairs. The value for the name “phoneNumbers” is an array whose elements are two objects.

    {

    “firstName”: “James”,

    “lastName”: “Jackson”,

    “age”: 52,

    “address”: “Kaiser Drive”,

    “city”: “Fremont”,

    “state”: “CA”,

    “postalCode”: “94555”,

    “phoneNumbers”: [

    { “Mobile”: “111-111-1111” },

    { “Landline”: “222-222-2222” }

    ]

    }

    This answer accepted by JavaNinja. on 19th September 2018 Earned 15 points.

    • 1175 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in JSON.

    JSON defines two data structures:

    1. Collection of name-value pairs (objects)
    2. Ordered list of values (array)

    JSON defines the following data types for the values:

    • Object – An object is an unordered set of name/value pairs. An object begins with ‘{‘ and ends with ‘}’. Each name is followed by ‘:’ (colon) and the name/value pairs are separated by ‘,’ (comma).
    • Array – An array is an ordered collection of values. An array begins with ‘[‘ and ends with ‘]’. Array values are separated by ‘,’ (comma).
    • String – A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.
    • Number –  A number is a signed decimal number that may contain a fractional part and may use exponential E-notation. JSON does not allow non-numbers like NaN, nor does it make any distinction between integer and floating-point.
    • Boolean – A boolean is either of the values true or false.
    • null – An empty value, using the word null.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1257 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in JSON.

    Benefits of JSON over XML are:

    • Lighter and faster than XML as on-the-wire data format
    • Object Representation – Information is presented in object notations, data is readily accessible as JSON objects in JavaScript code whereas XML data needed to be parsed and assigned to variables through DOM APIs
    • JSON objects supports multiple data types while XML data is all string
    • Retrieving values from JSON is as easy as reading from an object property in JavaScript code

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1279 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in JSON.

    JSON data format is used when writing JavaScript based application which includes browser extension and websites. JSON is often used as a common format for serializing and transmitting structured data in applications that communicate with each other over the Internet. It is primarily used to transmit data between a server and web application, serving as an alternative to XML.

    These applications are created using different programming languages and run in very different environments. JSON is suited to this scenario because it is an open standard, it is easy to read and write, and it is more compact than other representations. RESTful web services use JSON extensively as the format for the data inside requests and responses.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1299 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 18th September 2018 in JSON.

    JSON stands for JavaScript Object Notation. JSON is a lightweight text-based open standard data exchange format. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. JSON is language-independent, parsers are available in all programming languages.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 1356 views
    • 1 answers
    • 0 votes