反序列化JavaScript对象实例 [英] Deserializing JavaScript object instance

查看:90
本文介绍了反序列化JavaScript对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款大量使用JavaScript的应用。我试图包含一些面向对象的实践。在这次尝试中,我创建了一个类似的基本类:

I am working on an app that heavily uses JavaScript. I am attempting to include some object-oriented practices. In this attempt, I have created a basic class like such:

function Item() { this.init(); }
Item.prototype = {
  init: function () {
    this.data = {
      id: 0,
      name: "",
      description: ""     
    }
  },

  save: function() {
    alert("Saving...");
    $.ajax({
      url: getUrl(),
      type: "POST",
      data: JSON.stringify(this.data),
      contentType: "application/json"
    });
  }
}

我在我的应用中创建Item实例然后保存他们到本地存储如下:

I am creating Item instances in my app and then saving them to local storage like such:

Item item = new Item();
window.localStorage.setItem("itemKey", JSON.stringify(item));

在另一个页面上,或者在另一个时间,我正在从本地存储中重新搜索该项目,如: / p>

On another page, or at another time, I am retriving that item from local storage like such:

var item = window.localStorage.getItem("itemKey");
item = JSON.parse(item);
item.save();

不幸的是,保存功能似乎无法达到。在控制台窗口中,出现错误:

Unfortunately, the "save" function does not seem to get reached. In the console window, there is an error that says:

* save_Click
(匿名函数)
onclick *

*save_Click (anonymous function) onclick*

我有一种预感,(匿名函数)是控制台窗口说调用item.save(),但item是匿名类型,所以我试图访问匿名功能。我的问题是,我不知道如何再次将var item转换为Item类实例。有人可以告诉我吗?

I have a hunch that the "(anonymous function)" is the console window's way of saying "calling item.save(), but item is an anonymous type, so I am trying to access an anonymous function". My problem is, I'm not sure how to convert "var item" into an Item class instance again. Can someone please show me?

推荐答案

简答:

函数无法序列化为JSON。

Functions cannot be serialized into JSON.

说明:

JSON是一种基于JS文字语法子集的跨平台序列化方案。在这种情况下,它只能存储某些东西。每 http://www.json.org/

JSON is a cross-platform serialization scheme based on a subset of JS literal syntax. This being the case, it can only store certain things. Per http://www.json.org/ :


  • 对象:对象是一组无序的名称/值对。对象以{(左括号)开头,以}结尾(右大括号)。每个名称后跟:(冒号),名称/值对用(逗号)分隔。

  • 数组:数组是有序的值集合。数组以[(左括号)开头,以]结尾(右括号)。值以(逗号)分隔。

  • 值:值可以是双引号中的字符串,或数字,或true或false或null,或对象或数组。这些结构可以嵌套。

函数无法序列化为JSON,因为另一个非JS平台无法反序列化并使用它。反过来考虑一下这个例子。假设我的服务器上有一个包含属性和方法的PHP对象。如果我使用PHP的 json_encode()序列化该对象并且输出中包含了方法,那么我的JavaScript将如何能够解析和理解方法中的PHP代码,更不用说使用这些方法?

Functions cannot be serialized into JSON because another non-JS platform would not be able to unserialize and use it. Consider the example in reverse. Say I had a PHP object at my server which contained properties and methods. If I serialized that object with PHP's json_encode() and methods were included in the output, how would my JavaScript ever be able to parse and understand PHP code in the methods, let alone use those methods?

您在生成的JSON中看到的是函数的 toString()值你正在使用的平台。 JSON serilizer在序列化的任何内容上调用 toString(),这对于JSON来说是不合适的。

What you are seeing in your resulting JSON is the toString() value of the function on the platform you're using. The JSON serilizer calls toString() on anything being serialized which isn't proper for JSON.

我相信你的解决方案是停止在JSON /本地存储中存储实例。而是保存您在需要时设置回新实例的实例的相关数据。

I believe your solution is to stop storing instances in JSON/local storage. Rather, save pertinent data for an instance which you set back to a new instance when you need.

这篇关于反序列化JavaScript对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆