元素列表后面的JSON evel()错误:缺失

时间:2012-02-29 22:14:01

标签: javascript json

嗨我相对较新的JS,并在尝试运行以下内容时不断收到错误missing] after element list

有一个名为test.JSON的JSON文件:

{
"156644": {
    name: "name1",
    "street": "street1",
    "city": "city1"
},
"68656": {
    "name": "name2 ",
    "street": "street2",
    "city": "city1"
},
"388655": {
    "name": "name3",
    "street": "street3",
    "city": "city1"
},
"4564": {
    "name": "name4",
    "street": "street4",
    "city": "city1"
},
"6333": {
    "name": "name5",
    "street": "street5",
    "city": "city1"
}
 }

还有一些Javascript:

var myObject = eval("(" + $.getJSON("test.json") + ")");

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

getJSON功能:

  1. 将JSON解析为JavaScript对象。请勿触摸eval
  2. 是异步的,通过回调处理数据而不是返回值
  3. 所以:

    $.getJSON("test.json", handleData);
    
    function handleData(data) {
        console.log(data);
    }
    

    您的JSON数据中也存在语法错误。使用http://jsonlint.com/来查明它。