JS - JSON + XML

Ace your homework & exams now with Quizwiz!

Enter a string that can be given to JSON.parse which produces an array of three values: 1, 7, and 19.

"[1, 7, 19]" A JSON array is created with brackets surrounding the values, which are separated by commas. The JSON array should be surrounded by double quotes to create a string value.

What is the result of the following JSON.stringify call that uses two spaces for the space parameter? JSON.stringify({a:{b:1, c:3}}, null, ' '}

'{ "a": { "b": 1, "c": 3, } }'

Example of processing XML?

Data in XML can be accessed in the same way that elements in DOM *Using methods of getElementsByTagName("tag")[n].childNodes[m].nodeValue*

How to decipher JSON?

JavaScript Object Notation

What is a JSON array?

Ordered list of zero or more JSON values separated by commas and enclosed within brackets. Ex: [13, "blue"]

What does the following code return? JSON.stringify({a:1, b:2}, null, ' ')

Returns the string below: '{ "a": 1, "b": 2 }' When the spacer parameter is not specified and not an empty string, the output will also include newlines.

What is a JSON object?

Unordered list of zero or more name/value pairs separated by commas and enclosed within braces. E: {"Name" : "Joe", "Age" : 35}

What is the result of the following JSON.stringify call? JSON.stringify({a:"one", b:"two", c:"three"}, {["a", "c"]})

'{"a":"one", "c": "three"}' When the replacer parameter is an array, the JSON.stringify method only populates the final string with keys matching values in the array.

JSON syntax (5)?

- Keys are wraped into "double quotes" - Data is in name/value pairs - Data is separated by commas - Curly braces hold objects - Square brackets hold arrays

JSON data types (6)?

1) *Number*: double-precision floating point format in JS (not necessarily quoted) 2) *String*: double-quoted Unicode with backslash escaping 3) *Boolean*: true or false (not necessarily quoted) 4) *Array*: an ordered sequence of values 5) *Object*: an unordered collection of key:value pairs 6) *Null*: empty (not necessarily quoted)

Which method is used to convert JSON into object?

JSON.parse

Which method is used to convert object into JSON?

JSON.stringify

Which optional parameter can convert the string representation of a date into a JavaScript Date object?

The optional reviver function takes a key and value pair and returns a JavaScript representation of the value. A function used to transform the result. The function is called for each item. Any nested objects are transformed before the parent.

When the spacer parameter is not specified and not an empty string, what happens?

The output will also include newlines.

What does the following code do: JSON.parse( '[ 1, "two", null ]')?

Turns the passed JavaScript string '[ 1, "two", null]' into a JavaScript array containing three values, [ 1, "two", null].

What JSON.stringify(obj, *replacer*, space) parameter does?

replacer - Optional. Either a function or an array used to transform the result. The replacer is called for each item.

What JSON.stringify(obj, replacer, *space*) parameter does?

space - Optional. Either a String or a Number. A string to be used as white space (max 10 characters), or a Number, from 0 to 10, to indicate how many space characters to use as white space.

What string is returned from the call to JSON.stringify? JSON.stringify({date: new Date("2001-01-01")})

{ "date": "2001-01-01T00:00:00.000Z" } A JavaScript object such as Date is converted to a string.

What does the following code return? JSON.stringify({a:1, b:2, c:3}, ['a', 'b'])

'{"a":1, "b":2}'

What is JSON?

*JSON* is a string first of all JavaScript Object Notation is an efficient, structured format for data based on a subset of the JavaScript language.

What *JSON.parse()* method does? 2 parameters?

*JSON.parse(string, function)* - Creates a JavaScript object from a string containing JSON. 1) string - Required. A string written in JSON format 2) reviver function - Optional. A function used to transform the result. The function is called for each item. Any nested objects are transformed before the parent. - If the function returns a valid value, the item value is replaced with the transformed value - If the function returns undefined, the item is deleted

What *JSON.stringify()* does? 3 parameters?

*JSON.stringify(obj, replacer, space)* - Creates a string representation of any passed object by either calling the object's toJSON method if defined or recursively serializing all enumerable, non-function properties. (*literally converts object into JSON*) 1) obj - Required. The value to convert to a string 2) replacer - Optional. Either a function or an array used to transform the result. The replacer is called for each item. 3) space - Optional. Either a String or a Number. A string to be used as white space (max 10 characters), or a Number, from 0 to 10, to indicate how many space characters to use as white space.

What JSON.parse(string, *function*) parameter does?

*reviver function* - Optional. A function used to transform the result. The function is called for each item. Any nested objects are transformed before the parent. - If the function returns a valid value, the item value is replaced with the transformed value - If the function returns undefined, the item is deleted *Helpful when a JSON string represents a data type not available in JSON.*

What type of quotes should be used around JavaScript strings containing JSON notation? Why?

Single quotes so that the double quotes for strings and JSON object names do not need to be escaped. Ex: Use '{"name" : "Bob"}' instead of "{\"name\" : \"Bob\"}"

Write a JavaScript object that will be converted to '{"a":true,"b":"[ ]","c":null, "d":"string", "e":number}' by JSON.stringify.

{"a": true, "b": [], "c" : null , "d":"string", "e":number} A JavaScript object is created with braces surrounding the name/value pairs separated by commas.


Related study sets

2020A&P Quiz 1 Chapter 5 Integumentary System Study Set

View Set

Honan Nursing Management: Patients With Burn Injury

View Set

Ch. 31 - Infection Prevention and Management

View Set

Методологія зоологічного експерименту

View Set

Chapter 3 - Table Creation & Management

View Set