将自定义对象转换为JSON,然后返回到自定义对象? [英] Custom object to JSON then back to a custom object?

查看:193
本文介绍了将自定义对象转换为JSON,然后返回到自定义对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了与此非常相似的问题,但是我不能完全确定它们是否得到了清晰的回答-也许我有点笨拙,对不起.

I've seen very similar questions to this, but I can't quite decide if they was answered clearly - maybe I'm being a bit dense, sorry.

我想拥有自己的对象的便利性(和清晰度),将其称为CardboardBox().它不包含代码,仅包含数据.我想将其写入数据库并稍后再读回,但是显然,回读时它是类型Object().我唯一能想到的是:

I want to have the convenience (and clarity) of my own object, call it a CardboardBox(). It won't contain code, just data. I want to write this to a database and read it back later, but obviously, it is a type Object() when it's read back. All I can think of to find out what it used to be is:

  1. 具有一个我设置为CARDBOARD_BOX的成员变量type
  2. 实例化一个新的CarbardBox()并使用一个函数(在框中)将Object()的属性复制到新的CardboardBox()对象
  1. Have a member variable type that I set to CARDBOARD_BOX
  2. Instantiate a new CarbardBox() and use a function (in the box) to copy the properties of Object() to the new CardboardBox() object

是否有更好的方法?我很确定我可以更改实际类型.

Is there a better way of doing this? I'm pretty sure I can change the actual type.

function CardboardBox() { 
  this.type = "CARDBOARD_BOX"
  this.name = "No set";
  this.populate = new function(obj) {
    // populate this object with obj properties 
}

var box = new CarboardBox();  // CarboardBox
box.name = "My Box";
send = JSON.stringyfy(box);   
.
.
.
obj = JSON.parse(send);    // Object

if (obj.type == "CARDBOARD_BOX") {
  savedBox = new CardboardBox();
  savedBox.populate(obj);
}

预先感谢... 史蒂夫

Thanks in advance... Steve

[edit]我的测试代码.

[edit] My test code.

function CardboardBox(n) {
  this.name = n;
}

var box = new CardboardBox("My Box");
send = JSON.stringify(box); // JSON CarboardBox()

obj = JSON.parse(send, function fn(obj) { // Object() returned
  log("OB: "+obj.type);
  return obj.type === 'CardboardBox' ? new CardboardBox(obj) : CardboardBox; 
});     
console.log(obj);

输出为:

OB: undefined utils.js:40
OB: undefined utils.js:40
function CardboardBox(n) {
    this.name = n;
} 

推荐答案

一种可能的解决方案如下:

One possible solution is the following:

function CardboardBox(n) {
  if(typeof(n) == 'string') {
    //build from name string
    this.name = n;
  } else {
    //build from object
    this.name = n.name;
  }

  //add in this object's "type" in a place
  //that is unlikely to exist in other JSON strings
  this.__type = 'CardboardBox';
}

var box = new CardboardBox("My Box");
send = JSON.stringify(box), // JSON CarboardBox()
obj = JSON.parse(send, function(key, val) {
  //if this is an object, and is CardboardBox
  if(typeof(val) === 'object' && val.__type === 'CardboardBox')
      return new CardboardBox(val);

  return val;

  //or if your object is in a context (like window), and there are many of
  //them that could be in there, you can do:
  //
  //if(typeof(val) === 'object' && context[val.__type])
  //    return new context[val.__type](val);
});


console.log(obj);

基本上将对象类型存储在您知道的位置,以便以后在解析json时查找.如果您有多个对象,则可以在单个作用域中实例化,第二种解析方法可能更合适.这还将考虑JSON中不是 CarboardBox s的对象.

Basically store the object type in a place you know to look for later on when parsing the json. if you have multiple objects you can instantiate in a single scope the second parse method may be more appropriate. This also will account for objects in the JSON that are not CarboardBoxs.

编辑,这是该方法的 jsFiddle .

这篇关于将自定义对象转换为JSON,然后返回到自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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