top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Which function is used to convert a JSON text into an object?

0 votes
461 views
Which function is used to convert a JSON text into an object?
posted Jul 9, 2017 by Naveen Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

To convert JSON text into an object 'eval()' function is used.

Example

eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4

If the argument of eval() is not a string, eval() returns the argument unchanged. In the following example, the String constructor is specified, and eval() returns a String object rather than evaluating the string.

answer Jul 10, 2017 by Sumana
Similar Questions
+1 vote

Write a function which can take a random JSON and transform the data into multiple line human readable title value pairs.

For example:

Given JSON

{
        university: 'Oxford',
        batch: '2019-2020',
        address: {
            street: '144 Main',
            city: 'London',
            country: 'UK',
            contact: {
                fax: 'XXXXXX',
                phone: 'YYYYY'
            }
        },
        students: [{
            name: 'Jon Doe',
            age: '22'
        },{
            name: 'Mike Wilson',
            age: '32'
        },{
            name: 'David',
            age: '28'
        }]
}

Output:

university: Oxford
batch: 2019-2020
address street: 144 Main
address city: London
address country: UK
address contact fax: XXXXXX
address contact phone: YYYYY
students 1 name: Jon Doe
students 1 age: 22
students 2 name: Mike Wilson
students 2 age: 32
students 3 name: David
students 3 name: 28
...