包含 JSON 对象的数组,以大括号错误开头和结尾写入



我得到了一个"数组",里面有JSON对象(它不是一个实际的数组,因为它以大括号开头(:

{{"x1","x2"},{"y1","y2"},{"z1","z2"}}

如何使第一个和最后一个 { 花括号 } 变成方括号 [ ]? Javascript 不会将上述示例识别为数组并抛出错误。调用 JSON.stringify 或 JSON.parse 也不起作用,因为上面不是实际的 JSON/Array。只有当它有[方括号]时,它才真正工作,因为那时它是一个包含JSON对象的数组。这样:

[{"x1","x2"},{"y1","y2"},{"z1","z2"}]

我正在考虑先将其制作成字符串,然后分别用 [ 和 ] 替换第一个和最后一个字符,但是调用 String(value(,其中 value 是第一个"数组",根本不会削减它,因为它认为它是一个 JSON 并抛出意外的令牌如果我什至声明它。

如果您尝试创建 JSON 对象数组,您可以执行以下操作:

const object = new Array(
{ 'x1': 'x2' },
{ 'y1': 'y2' },
{ 'z1': 'z2' }
);
console.log( object[0] );

在控制台中预期以下结果:

{
x1: "x2"
}

希望对您有所帮助。

正如评论中已经指出的那样,您的输入 JSON 根本不是真正的 JSON - 您需要一个自定义解析器。以下是从Haxe的haxe.format.JsonParser快速打乱的内容:

var Std = function() { };
Std.parseInt = function(x) {
if(x != null) {
var _g = 0;
var _g1 = x.length;
while(_g < _g1) {
var i = _g++;
var c = x.charCodeAt(i);
if(c <= 8 || c >= 14 && c != 32 && c != 45) {
var nc = x.charCodeAt(i + 1);
var v = parseInt(x,nc == 120 || nc == 88 ? 16 : 10);
if(isNaN(v)) {
return null;
} else {
return v;
}
}
}
}
return null;
};
var StringBuf = function() {
this.b = "";
};
var JsonParser = function(str) {
this.str = str;
this.pos = 0;
};
JsonParser.prototype = {
doParse: function() {
var result = this.parseRec();
var c;
while(true) {
c = this.str.charCodeAt(this.pos++);
if(!(c == c)) {
break;
}
switch(c) {
case 9:case 10:case 13:case 32:
break;
default:
this.invalidChar();
}
}
return result;
}
,parseRec: function() {
while(true) switch(this.str.charCodeAt(this.pos++)) {
case 9:case 10:case 13:case 32:
break;
case 34:
return this.parseString();
case 123:
var arr = [];
var comma = null;
while(true) switch(this.str.charCodeAt(this.pos++)) {
case 9:case 10:case 13:case 32:
break;
case 44:
if(comma) {
comma = false;
} else {
this.invalidChar();
}
break;
case 125:
if(comma == false) {
this.invalidChar();
}
return arr;
default:
if(comma) {
this.invalidChar();
}
this.pos--;
arr.push(this.parseRec());
comma = true;
}
break;
default:
this.invalidChar();
}
}
,parseString: function() {
var start = this.pos;
var buf = null;
var prev = -1;
while(true) {
var c = this.str.charCodeAt(this.pos++);
if(c == 34) {
break;
}
if(c == 92) {
if(buf == null) {
buf = new StringBuf();
}
var s = this.str;
var len = this.pos - start - 1;
buf.b += len == null ? s.substr(start) : s.substr(start,len);
c = this.str.charCodeAt(this.pos++);
if(c != 117 && prev != -1) {
buf.b += String.fromCodePoint(65533);
prev = -1;
}
switch(c) {
case 34:case 47:case 92:
buf.b += String.fromCodePoint(c);
break;
case 98:
buf.b += String.fromCodePoint(8);
break;
case 102:
buf.b += String.fromCodePoint(12);
break;
case 110:
buf.b += String.fromCodePoint(10);
break;
case 114:
buf.b += String.fromCodePoint(13);
break;
case 116:
buf.b += String.fromCodePoint(9);
break;
case 117:
var uc = Std.parseInt("0x" + this.str.substr(this.pos,4));
this.pos += 4;
if(prev != -1) {
if(uc < 56320 || uc > 57343) {
buf.b += String.fromCodePoint(65533);
prev = -1;
} else {
buf.b += String.fromCodePoint(((prev - 55296 << 10) + (uc - 56320) + 65536));
prev = -1;
}
} else if(uc >= 55296 && uc <= 56319) {
prev = uc;
} else {
buf.b += String.fromCodePoint(uc);
}
break;
default:
throw new ("Invalid escape sequence \" + String.fromCodePoint(c) + " at position " + (this.pos - 1));
}
start = this.pos;
} else if(c != c) {
throw new ("Unclosed string");
}
}
if(prev != -1) {
buf.b += String.fromCodePoint(65533);
prev = -1;
}
if(buf == null) {
return this.str.substr(start,this.pos - start - 1);
} else {
var s1 = this.str;
var len1 = this.pos - start - 1;
buf.b += len1 == null ? s1.substr(start) : s1.substr(start,len1);
return buf.b;
}
}
,invalidChar: function() {
this.pos--;
throw "Invalid char " + this.str.charCodeAt(this.pos) + " at position " + this.pos;
}
};
JsonParser.parse = function(s) {
return new JsonParser(s).doParse();
}

如果有人给你这个,你真的应该回到源头,告诉他们他们没有为你提供有效的输入。

我避免了破解糟糕的源数据。 在源头修复它,而不必在使用它的任何地方处理它。

相关内容

最新更新