Hello. There is such code:

class User { constructor(id, username, date){ this.id = id; this.username = username; this.date= date; } } let myUsers = [ new User(1, 'Mark', new Date('03.01.2016')), new User(2, 'Jacob', new Date('02.02.2016')), new User(3, 'Petr', new Date('01.02.2016')) ]; 

I turn this array into JSON and store it in local storage.

 localStorage.setItem('myUsers', JSON.stringify(myUsers)); 

Then get out and perform the parsing

 let users = localStorage.getItem('myUsers'); users = JSON.parse(users); 

When I try to display a formatted date

 users.forEach(function(user){ console.log(user.date.getDate()); }); 

reports an error

 Uncaught TypeError: user.date.getDate is not a function 

As I understand it, this happens because after parsing I get a string, not an object with a date. Actually this is the question. How to get the opportunity to work with the properties of the object in this case?

  • you apparently for some reason think that when you call JSON.parse() get instances of objects of class User , but it is not. - teran

2 answers 2

JSON.stringify () Functions are not valid data in JSON, so they will not work. In addition, some objects, such as the Date object, will become strings after applying JSON.parse ().

JSON.stringify(myUsers) will create a JSON string that looks like this:

 "[{"id":1,"username":"Mark","date":"2016-02-29T17:00:00.000Z"},{"id":2,"username":"Jacob","date":"2016-02-01T17:00:00.000Z"},{"id":3,"username":"Petr","date":"2016-01-01T17:00:00.000Z"}]" 

Those. each User object will have string , number , boolean , object and array properties. And after JSON.parse you get an array of Object (not User ) with the fields id , username , date .

 [Object, Object, Object] 

Those. need a method to create User objects (something like deserialization will work)

 class User { constructor(id, username, date){ this.id = id; this.username = username; this.date = date; } static fromJson(json) { var data = JSON.parse(json); // Парсим поля var date = new Date(data.date); // создает объект Date из строки return new User(data.id, data.username, date); // Создаем новый инстанс } }; Пример использования: const user1 = new User(1, 'Mark', new Date('03.01.2016')); const serialize = JSON.stringify(user1); //.toJson(); const _user1 = User.fromJson(serialize); console.log(_user1.date.getDate()); // 1 

To "serialize" only the necessary properties, you can also write the toJson method, inside which you already call JSON.stringify

    In JSON.parse() you can pass a second parameter to the function that will be called for each key-value pair:

     const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/; function reviver(key, value) { if (typeof value === "string" && dateFormat.test(value)) { return new Date(value); } return value; } const text = '{ "date": "2016-04-26T18:09:16Z" }'; const obj = JSON.parse(text, reviver); 

    Source of

    Or, if you know the name of the property in which the date is stored, you can simply check the value of the key argument.

    Similarly, you can transform the results of parsing JSON into any object you need.

    • here the question is more about the deserialization of a specific User class, and not about the date parsing from a string in Date - teran
    • Then the easiest way to re-go through the results and create the necessary objects through the User constructor: JSON.parse(json).map(data => new User(data.id, data.username, new Date(data.date))) - Timurib