Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将JSON响应转换为映射_Javascript_Jquery_Arrays_Json_Ajax - Fatal编程技术网

Javascript 将JSON响应转换为映射

Javascript 将JSON响应转换为映射,javascript,jquery,arrays,json,ajax,Javascript,Jquery,Arrays,Json,Ajax,我有一个正在修改的工作代码 let table_specs = {'columns': [ {'name': 'Col1', 'width': 7, ....}, {'name': 'Col2', 'width': 8, .....}, ........ {'name': 'Coln', 'width': 30, .

我有一个正在修改的工作代码

   let table_specs = {'columns': [ {'name': 'Col1', 'width': 7, ....},
                             {'name': 'Col2', 'width': 8, .....},
                             ........ 
                             {'name': 'Coln', 'width': 30, ....}]}
   foo(table_specs)

   function foo(table_specs){
      for (clmn of table_specs.columns){
        // do something
      }
   }
我试图修改程序,使表规格存储在JSON文件中,并通过ajax调用提取它们

z_grid_specs.json

{
"grid1":"{'columns': [ {'name': 'Col1', 'width': 7, ....}, ... {'name': 'Coln', 'wiith': 8, ...}]}"
}
服务器上的.js

    var pjson_grid = require('../startup/z_grid_specs.json');
    router.get('/grid_specs', (req, res)=> {   
        res.json(pjson_grid)
    })
我呼吁:

   var table_specs={}
   $.ajax({
    type: 'GET',
    contentType: 'application/json',
    url: '/session/grid_specs',
    success:function(response_data_json) {
        console.log(response_data_json.grid1)
        table_specs = response_data_json.grid1
        foo(table_specs)  
    }
  });
我可以验证ajax调用是否返回了一个正确的数据,该数据看起来像原始代码中的数组。但我在下一步遇到了一个错误:

表\u规格列不适用

如果我使用JSON.stringify(response\u data\u JSON.grid1),我会得到同样的错误

如果我使用JSON.parse(response\u data\u JSON.grid1),我会得到:

位置1处JSON中的“意外标记”


删除JSON文件中的双引号:

{
    "grid1": {'columns': [ {'name': 'Col1', 'width': 7, ....}, ... {'name': 'Coln', 'wiith': 8, ...}]}
}

grid1
键的值为
字符串
,您需要它是
对象

您在z_grid_specs中存储的任何内容。json不是/无效的JSON@Andreas. 因为引用?我试过了,也试过了。Same result.JSON为格式定义了一组规则。z_grid_specs.json的内容违反了这些规则。