简体   繁体   English

当没有“:”时,“未捕获的SyntaxError:意外令牌:”吗?

[英]“Uncaught SyntaxError: Unexpected token :” when there is no “:”?

Given JSON data without indexes : 给定不带索引的JSON数据:

var knol = [
    {"hant":"火","status":1,"rank":30,"score": 1},
    {"hant":"人","status":1,"rank":33,"score": 2}, 
    {"hant":"山","status":1,"rank":60,"score": 5}, 
    {"hant":"是","status":1,"rank":50,"score": 3}
];

Given a JS function to recreate the data with indexes : 给定一个JS函数以使用索引重新创建数据:

var addIndex = function(object, key1, key2){
    var data = object;
    var newData = "[";

    for (var i in data) {
        if (!key2) {
            var newIndex = data[i][key1]
        } else {
            var newIndex = data[i][key1] + data[i][key2]
        }

        newData = newData + "\"" + newIndex + "\":" + JSON.stringify(data[i]) +",";
    }

    newData = newData.concat("]").replace(",]", "]");
    console.log(newData);
    newData = JSON.parse(newData);
    return newData;
};

knol = addIndex(knol, "hant");

I get the error "Uncaught SyntaxError: Unexpected token :" , while there is NO ":" at the indicated place in the code. 我收到错误信息"Uncaught SyntaxError: Unexpected token :" ,而代码中的指定位置没有“:”。 This seems to crash my larger script. 这似乎使我的大脚本崩溃了。 Any idea what is going on ? 知道发生了什么吗?

Fiddle : http://jsfiddle.net/n9pf6/2/ 小提琴: http : //jsfiddle.net/n9pf6/2/

The intermediate JSON is incorrect, so the final JSON.parse is failing. 中间JSON不正确,因此最终JSON.parse失败。 The intermediate text is ["火":{"hant":"火",.. , as reported on the console, and it is indeed incorrect, as arrays don't have keys. 中间文本为["火":{"hant":"火",.. ,如控制台上所报告的那样,的确是不正确的,因为数组没有键。

Just build/rebuild the entire object-graph as a JS object and then use JSON.stringify once on it - or, in this case, never, as the end result will be a JS object anyway! 只需将整个对象图作为JS对象构建/重建,然后在其上使用JSON.stringify一次-否则,在这种情况下,永远不要使用,因为最终结果还是一个JS对象!

var addIndex = function(data, key1, key2){
    var newData = {}; // new JS object - there is /no/ JSON here.
    var newIndex;
    for(var i in data){
        if(!key2){
           newIndex = data[i][key1];
        }else{
           newIndex = data[i][key1] + data[i][key2];
        }
        // add data to new object with appropriate property/key
        // and do NOT manually build JSON text.
        newData[newIndex] = data[i];
    }
    // return object directly; it's not JSON text, no need to parse it!
    return newData;
};

You need to create an object ( {…} ), not an array ( […] ). 您需要创建一个对象( {…} ),而不是一个数组( […] )。 This would work: 这将工作:

var newData = "{";
for(var i in data){
    if(!key2){ var newIndex = data[i][key1] }else{ var newIndex = data[i][key1]+ data[i][key2]}
    newData = newData + "\""+newIndex+"\":"+JSON.stringify(data[i])+",";
    }
newData = newData.concat("}").replace(",]", "]").replace(",}", "}");

However, I'd strongly recommend just manipulating the data directly rather that trying to manually construct a JSON string from its various parts. 但是,我强烈建议您直接处理数据,而不要尝试从各个部分手动构造JSON字符串。

Here is a simple implementation: 这是一个简单的实现:

var addIndex = function(object, key1, key2){
    var data = object, newData = {}, newIndex;
    for(var i in data){
        if(!key2){ 
            newIndex = data[i][key1];
        }else{ 
            newIndex = data[i][key1]+ data[i][key2];
        }
        newData[newIndex] = data[i];
    }
    console.log(newData);

    return newData;
};

Demonstration 示范

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

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