程序go 2020-03-13 17:17 采纳率: 100%
浏览 148

将JSON响应转换为地图

I have a working code that I am trying to modify.

   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
      }
   }

I am trying to modify the program to have table_specs stored in JSON file and pulled them via ajax call.

z_grid_specs.json

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

.js on a server

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

and i call:

   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)  
    }
  });

I can verify that ajax called returns a proper data that looks like an array in an original code. But I get an error on a next step:

table_specs.columns is not iterable

I get the same error if I use JSON.stringify(response_data_json.grid1).

If i use JSON.parse(response_data_json.grid1) i get:

Unexpected token ' in JSON at position 1

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2020-03-13 18:13
    关注

    Remove the double quotes in your JSON file:

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

    The grid1 key has a value as a string, and you need it to be an object

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏