JavaScript - 使用方法将对象保存为字符串

标签 javascript

我一直在寻找一种方法来做到这一点,但似乎找不到任何东西,我有不同的配置对象,我需要将它们保存为变量中的文本,以便稍后进行一些处理,这是一个示例:

对象:

args.config.config = {
        next: null,
        final:[],
        delimiter: '~', header: false,
        step: function (row) {
            var item = {
                'line_code': row.data[0][0],
                'order': row.data[0][1]
            }
            args.config.config.final.push(item);
        },
        complete: function (result) {
            console.log('Reading data completed. Processing.');
            return args.config.config.next(null, args.config.config.final);
        },
        error: function () {
            console.log('There was an error parsing');
        }'
    }

我需要将其保存为字符串,例如:

args.config.config = "{object goes here}";

如果不将所有内容都放在一个巨大的行上或添加换行符,因为稍后将对其进行解析以在配置中使用,这会把事情搞砸,有什么想法吗?

更新: 所以把它们变成文本可能不是最好的解决方案,这些配置将存储在一个mongo数据库中,所以它可能会按原样使用它们(我还没有尝试过)。

我遇到的另一个问题是在配置对象中我有这个:

final.push(item)

return next(null, final)

将使用配置对象在另一个文件中定义:

其他文件:

exports.parse = function(args, next){//next is what I need to call in the config

    var final = []; //this is the final referred to in the config object
    ....
    Baby.parse(data, args.config)
}

因此 return next(null, final) 和 final.push(result) 必须引用新文件中的 var/函数,但我不知道如何让它工作,这就是我有在配置对象中添加一个最终数组和一个 null next 函数,然后像这样分配它:

exports.parse = function(args, next){
    args.config.next = next;
    ....
    Baby.parse(data, args.config)
}

对象用丑陋的一行调用它:

   return args.config.config.next(null, args.config.config.final);

如果有人能解决这个问题,我们将不胜感激。

最佳答案

如果你使用 JSON.stringify with a "replacer" function JSON.parse with a "reviver" function连同 new Function() ,你可以做到:

我不确定我是否在关注您提出的第二个(更新的)问题。将对象解析回对象后,为什么不能在调用对象的任何方法之前将 nextfinal 属性初始化为有效对象?您甚至可以在该方法中添加测试,在返回任何内容之前检查 finalnext 是否存在。

var myObj = {
        next: null,
        final:[],
        delimiter: '~', 
        header: false,
        step: function (row) {
            var item = {
                'line_code': row.data[0][0],
                'order': row.data[0][1]
            };
            args.config.config.final.push(item);
        },
        complete: function (result) {
            console.log('Reading data completed. Processing.');
            return args.config.config.next(null, args.config.config.final);
        },
        error: function () {
            console.log('There was an error parsing');
        }
    };

// Stringify the object using a replacer function that will explicitly
// turn functions into strings
var myObjString = JSON.stringify(myObj, function(key, val) {
        return (typeof val === 'function') ? '' + val : val;
});

// Now, parse back into an object with a reviver function to
// test for function values and create new functions from them:
var obj = JSON.parse(myObjString, function(key, val){
  
    // Make sure the current value is not null (is a string)
    // and that the first characters are "function"
    if(typeof val === "string" && val.indexOf('function') === 0){

      // Isolate the argument names list
      var start = val.indexOf("(") + 1;
      var end = val.indexOf(")");     
      var argListString = val.substring(start,end).split(",");
      
      // Isolate the body of the function
      var body = val.substr(val.indexOf("{"), val.length - end + 1);
      
      // Construct a new function using the argument names and body
      // stored in the string:
      return new Function(argListString, body);
      
    } else {
      // Non-function property, just return the value
      return val;
    } 
  }
);

// Test the method:
obj.error(); // 'There was an error parsing' is written to console.

// Examine the object:
console.log(obj);

关于JavaScript - 使用方法将对象保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40875630/

相关文章:

javascript - 迭代 JSON 对象数组

c# - Selenium 2 WebDriver 未按预期评估更新的 DOM

javascript - Lodash 是否捆绑/包含 Underscore?

javascript - XSS javascript,利用检查

javascript - 如何检查 iframe 是否正在加载元素?

javascript - Google 喜欢下拉菜单

javascript - Three.js 将map分配给object.children[0]会改变整个对象的map

javascript - 我希望子菜单在鼠标移到它上面时保持打开状态

javascript - 如何更改 eslint 设置以了解绝对导入?

Javascript 创建 next() 函数 OOP 方式