简体   繁体   English

map json 密钥到 javascript object

[英]map json keys to javascript object

I need to parse a json file into a javascript object.我需要将 json 文件解析为 javascript object。 I'm using GCC (Closure Compiler) which might mangle my object names, so I need to refer to the json keys as strings when importing or exporting the json file. I'm using GCC (Closure Compiler) which might mangle my object names, so I need to refer to the json keys as strings when importing or exporting the json file.

ie importing:即进口:

var jsonobj = JSON.parse(json_object_text);
myobj.member1 = jsonobj['Key 1'];
myobj.member2 = jsonobj['Key 2'];

and exporting:和出口:

jsonobj['Key 1'] = myobj.member1;
jsonobj['Key 2'] = myobj.member2;
var json_object_text = JSON.stringify(jsonobj);

But doing that is tedious.但这样做很乏味。 And I would like to only define the keys as text strings once.而且我只想将键定义为文本字符串一次。

So I tried to define a constant object which is kind of mapping from javascript object to the json schema.所以我尝试定义一个常量 object,它是从 javascript object 到 Z466DEEC76ECDF5FCA6D38571F6324 模式的映射。

// Schema definition for import/export of confc.json.
/** @const */ var dbs = {
    $schema: '$schema',
    $seq: '$seq',
    channel: {
      key: 'chan',
      id: 'id',
      name: 'txt',
      value: 'val'
    }
}

But then I still had to do a tedious import/export function:但后来我仍然不得不做一个乏味的导入/导出 function:

    export_db: function (db) {
      return db && {
        [dbs.$schema]: db.$schema,
        [dbs.$seq]: db.$seq,
        [dbs.channel.key]: db.channel.map(function (e, i) {
          var s = dbs.channel; // schema object
          return {
            [s.id]: util.toInt(e.id),
            [s.name]: e.name,
            [s.value]: e.value
          };
        }),
    import_db: function (js) {
      js = js || {};
      // wrapper for .map() function which works if array is undefined.
      function dbmap(arr_in, func) {
        arr_in = arr_in || [];
        return arr_in.map(func);
      }
      return {
        $schema: js[dbs.$schema],
        $seq: js[dbs.$seq],
        channel: dbmap(js[dbs.channel.key], function (e, i) {
          var s = dbs.channel;
          return {
            id: e[s.id],
            name: e[s.name],
            value: e[s.value]
          };
        }),

This seems to work OK, but would someone please show me a cleaner way to do this?这似乎工作正常,但有人可以告诉我一个更清洁的方法吗?

I would like to use an object prototype/constructor like in the examples given here .我想使用 object 原型/构造函数,就像这里给出的示例一样。 But I don't see any examples that take into account how GCC might mangle the objects member names.但我没有看到任何考虑到 GCC 可能如何破坏对象成员名称的示例。 Also I have nested objects (no functions, just text and numbers).我也有嵌套对象(没有函数,只有文本和数字)。

JSON is inherently already in object format (JSON = JavaScript Object Notation). JSON 本质上已经是 object 格式(JSON = JavaScript Z497031794414A552435ZNotation)1BACB54。

var jsonobj = JSON.parse(json_object_text) should be all you need to access the object you just created called jsonobj . var jsonobj = JSON.parse(json_object_text)应该是您访问刚刚创建的名为jsonobj的 object 所需的全部内容。 Any members of that object are accessible just as if you had defined them in an constructor or anywhere else.该 object 的任何成员都可以访问,就像您在构造函数或其他任何地方定义它们一样。 Is there a reason you're remapping it to a different object?您是否有理由将其重新映射到不同的 object?

If the data source is external, or if you need to share the data externally, the way that Closure supports this via externs and @export annotations .如果数据源是外部的,或者如果您需要在外部共享数据,Closure 通过externs@export注释支持这一点的方式。 User contributed externs are a great source for examples. 用户贡献的外部人员是示例的重要来源。

It might look like this, depending on your environment;根据您的环境,它可能看起来像这样;

// externs/config.js // 外部/config.js

/**
 * @typedef {{
 *   foo: (string|undefined),
 *   [...]
 * }}
 */
Bar.Config;

/**
 * @typedef {{
 *   bar: (string|undefined),
 *   [...]
 * }}
 */
Bar.Output;

// main.js // main.js

/**
 * @param {Bar.Config} config 
 * @return {Bar.Output}
 */
const bar = function(config) {
//  [...]
  return output;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM