Question

In: Computer Science

JS Suppose you have an object named obj. How do you serialize it to a JSON...

JS

Suppose you have an object named obj. How do you serialize it to a JSON string with each level indented two spaces? This action is the inverse of JSON.parse.

Solutions

Expert Solution

Method 1:

You need json2.js search in github

 JSON.stringify(value, replacer, space)
        value       any JavaScript value, usually an object or array.

       replacer    an optional parameter that determines how object
                    values are stringified for objects. It can be a
                    function or an array of strings.

       space       an optional parameter that specifies the indentation
                    of nested structures. If it is omitted, the text will
                    be packed without extra whitespace. If it is a number,
                    it will specify the number of spaces to indent at each
                    level. If it is a string (such as '\t' or ' '),
                    it contains the characters used to indent at each level.

       This method produces a JSON text from a JavaScript value.

2. Method 2:

In angularJS

angular.toJson(obj, pretty);

obj: Input to be serialized into JSON.

pretty(optional):
If set to true, the JSON output will contain newlines and whitespace. If set to an integer, the JSON output will contain that many spaces per indentation.

I have used the angular approach previously.


Related Solutions

In Marvin JS Editor how do you make OH into OD?
In Marvin JS Editor how do you make OH into OD?
how do you send the values of a method to the main in obj java. im...
how do you send the values of a method to the main in obj java. im having syntax errors and im not sure how to fix them or what the problem is. class Order { private int orderNum; private double orderAmount; private double orderDiscount;    public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double getOrderAmount() { return orderAmount; } public double getOrderDisc() { return orderDiscount; } public static void...
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment...
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment This assignment aligns with the following objectives: Identify the differences between these three modes of interacting with data, i.e. web forms, programs, and APIs Recognize well-formed JSON objects Parse JSON objects Details Imagine that using the API provided by a service like Yelp (let's call it YelpYou) you have extracted information about Sichuan Dragon, which has three branches (restaurants) in New York City. The...
Consider a “system with two lenses in tandem”. Suppose that you have an object that is...
Consider a “system with two lenses in tandem”. Suppose that you have an object that is located P1to the left of a lens with F1 focal length. Then suppose that you have a second lens with F2 focal length, and you place this lens D downstream of the first lens. Let us say that the final image of the optical system forms Q2 past the second lens. The two lenses are one after another, or in tandem. (a) Draw a...
how do we use dictionaries to get json api in MIT app inventor
how do we use dictionaries to get json api in MIT app inventor
How is the speed of an object in space measured? Also more importantly how do you...
How is the speed of an object in space measured? Also more importantly how do you measure your own speed in space? On the road we use a speedometer which tells us the speed easily. How is it done in space?
You should assume that there will be an object file named “foreignsub.o” which implements a function...
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line. int sub(int n, int *A, int *B, int *C); However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three arrays. You are also given...
How do I make an HTML JSON button that when clicked will say Hello World!
How do I make an HTML JSON button that when clicked will say Hello World!
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
Suppose an object is 4 sided marked as {1,2,3,.., 4. Now you toss this object 4...
Suppose an object is 4 sided marked as {1,2,3,.., 4. Now you toss this object 4 times. Let nS be the number of outcomes in this sample space. Let A be the event consisting of all outcomes in which 2 occurs at least once. How many outcomes are there in A? It is whole number. Computer follows comma rule when entering large numbers. For example, 1123 is entered as 1,123
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT