node.js JSON.parse Reviver没有报告重复密钥吗? [英] node.js JSON.parse reviver doesn't report duplicate keys?

查看:122
本文介绍了node.js JSON.parse Reviver没有报告重复密钥吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON解析器,它将检测并保存重复的密钥.我在node.js中将JSON.parse()与齐磊器一起使用,希望当我得到重复的密钥时可以告诉我.但是事实并非如此.还有另一种方法吗?是否有更好的JSON解析器来处理齐整器或其他参数中的重复键?

I'm trying to use a JSON parser which will detect and save duplicate keys. I'm using JSON.parse() in node.js with a reviver, which I am hoping will tell me when I'm getting a duplicate key. However it doesn't. Is there another way? Is there a better JSON parser which handles duplicate keys in a reviver or other argument?

"use strict";

try {
    var content = '{"value": "a", "value": "b", "value": "c" }';
    console.log(content);
    var parsed = JSON.parse(content, function(k, v) {
        console.log(k+"="+JSON.stringify(v));
        return v;
    });
} catch (e) {
    console.log(e);
}

输出为:

{"value": "a", "value": "b", "value": "c" }
value="c"
={"value":"c"}

推荐答案

JSON.parse()以相同的方式解析字符串,无论您是否提供reviver函数(换句话说,当传入reviver时,它都不会切换到流解析器" ).提供reviver函数只是一种方便,这样就不必自己编写必要的循环.

JSON.parse() parses the string the same way whether or not you provide a reviver function (in other words, it does not switch to a "streaming parser" when a reviver is passed in). Providing a reviver function is just a convenience so as not to have to write the necessary loops yourself.

在npm上有 个流JSON解析器,例如: clarinet JSONStream 双簧管.这是针对那些3的小测试:

There are some streaming JSON parsers on npm, for example: clarinet, JSONStream, and oboe. Here's a little test for those 3:

var clarinet = require('clarinet').parser();
var JSONStream = require('JSONStream').parse('*', function (value, path) {
  return { key: path[path.length - 1], value: value, path: path }
});
var rs = new (require('stream').Readable)();
rs._read = function(n) {};
var oboe = require('oboe')(rs);

var content = '{"value": "a", "value": "b", "value": "c" }';

clarinet.onopenobject = clarinet.onkey = function(k) {
  console.log('clarinet key =', k);
};
clarinet.onvalue = function(v) {
  console.log('clarinet value =', v);
};
clarinet.write(content).close();

JSONStream.on('data', function(d) {
  console.log('JSONStream data:', arguments);
}).end(content);

oboe.on('node:*', function(n) {
  console.log('oboe node:', arguments);
});
rs.push(content);
rs.push(null);

// output:
// clarinet key = value
// clarinet value = a
// clarinet key = value
// clarinet value = b
// clarinet key = value
// clarinet value = c
// JSONStream data: { '0': { key: 'value', value: 'a', path: [ 'value' ] } }
// JSONStream data: { '0': { key: 'value', value: 'b', path: [ 'value' ] } }
// JSONStream data: { '0': { key: 'value', value: 'c', path: [ 'value' ] } }
// oboe node: { '0': 'a', '1': [ 'value' ], '2': [ { value: 'a' }, 'a' ] }
// oboe node: { '0': 'b', '1': [ 'value' ], '2': [ { value: 'b' }, 'b' ] }
// oboe node: { '0': 'c', '1': [ 'value' ], '2': [ { value: 'c' }, 'c' ] }
// oboe node: { '0': { value: 'c' }, '1': [], '2': [ { value: 'c' } ] }

这篇关于node.js JSON.parse Reviver没有报告重复密钥吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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