parse the object in javascript - Stack Overflow
How to parse a string in JavaScript? - Stack Overflow
javascript - How does JSON.parse() work? - Stack Overflow
Parse JSON String into a Particular Object Prototype in JavaScript - Stack Overflow
Videos
A Javascript object is a data type in Javascript - it's have property and value pair as you define in your first example.
var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};
Now What is Json : A JSON string is a data interchange format - it is nothing more than a bunch of characters formatted a particular way (in order for different programs to communicate with each other)
var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';
so it's is a String With json Format.
and at last JSON.parse() Returns the Object corresponding to the given JSON text.
Here is my explanation with a jsfiddle.
//this is already a valid javascript object
//no need for you to use JSON.parse()
var obj1 = {"name":"abcd", "details":"1234"};
console.log(obj1);
//assume you want to pass a json* in your code with an ajax request
//you will receive a string formatted like a javascript object
var str1 = '{"name":"abcd", "details":"1234"}';
console.log(str1);
//in your code you probably want to treat it as an object
//so in order to do so you will use JSON.parse(), which will
//parse the string into a javascript object
var obj2 = JSON.parse(str1);
console.log(obj2);
JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML.
The current answers contain a lot of hand-rolled or library code. This is not necessary.
Use
JSON.parse('{"a":1}')to create a plain object.Use one of the standardized functions to set the prototype:
Object.assign(new Foo, { a: 1 })Object.setPrototypeOf({ a: 1 }, Foo.prototype)
See an example below (this example uses the native JSON object). My changes are commented in CAPITALS:
function Foo(obj) // CONSTRUCTOR CAN BE OVERLOADED WITH AN OBJECT
{
this.a = 3;
this.b = 2;
this.test = function() {return this.a*this.b;};
// IF AN OBJECT WAS PASSED THEN INITIALISE PROPERTIES FROM THAT OBJECT
for (var prop in obj) this[prop] = obj[prop];
}
var fooObj = new Foo();
alert(fooObj.test() ); //Prints 6
// INITIALISE A NEW FOO AND PASS THE PARSED JSON OBJECT TO IT
var fooJSON = new Foo(JSON.parse('{"a":4,"b":3}'));
alert(fooJSON.test() ); //Prints 12
The JSON.parse function accepts an optional DateTime reviver function. You can use a function like this:
dateTimeReviver = function (key, value) {
var a;
if (typeof value === 'string') {
a = /\/Date\((\d*)\)\//.exec(value);
if (a) {
return new Date(+a[1]);
}
}
return value;
}
Then call
JSON.parse(somejsonstring, dateTimeReviver);
And your dates will come out right.
There is no standard JSON representation of dates. You should do what @jAndy suggested and not serialize a DateTime at all; just send an RFC 1123 date string ToString("r") or a seconds-from-Unix-epoch number, or something else that you can use in the JavaScript to construct a Date.
Naively,
JSON.parse(str.replace(/"/g,'"'));
will work, but you should figure out why your double quotes are getting turned into HTML entities.
Description
This will allow for the numerical ascii character of any character. However, if you only have quotations to remove it isn't too much to run a replace.
var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]";
// regex to get the ascii number
var r = /\&#(\d\d);/gi;
// replaces each numerical ascii character with their character representation
str = str.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp)); } );
console.log(JSON.parse(str));