【问题标题】:node - Converting to JSON fails, but can print in console节点 - 转换为 JSON 失败,但可以在控制台中打印
【发布时间】:2015-04-15 00:36:40
【问题描述】:

我正在尝试将数组转换为 JSON 以发送给客户端。以下是控制台中的数据:

[ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],                                                                                        
LU: [ true,                                                                                                                                                            
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
false,                                                                                                                                                               
false,                                                                                                                                                               
false ],                                                                                                                                                             
SE: [ false, false, false ] ] 

但是当我运行它时(res 是一个 express.js 套接字):

console.log(st.bot.serverStatus);
res.send(JSON.stringify(st.bot.serverStatus));

我在控制台中得到了预期的输出,但我从网络浏览器得到了[]。我做错了什么?

PS:我无法更改元素的格式,它们是通过这种方法生成的:

        if(st.bot.serverStatus[tmp.country] !== undefined) {
            st.bot.serverStatus[tmp.country][st.bot.serverStatus[tmp.country].length] = alive;
        } else {
            st.bot.serverStatus[tmp.country] = [ alive ];
        }

【问题讨论】:

  • 你能添加你从服务器得到的输出吗?
  • 你是如何初始化st.bot.serverStatus的?
  • st.bot.serverStatus = [];

标签: javascript json node.js express


【解决方案1】:

SE:LE: 的语法无效,因为它是数组而不是对象。将最外层的[] 更改为{} 或将: 更改为,

【讨论】:

    【解决方案2】:

    使用任一:

    console.log(JSON.stringify({ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],                                                                                        
    LU: [ true,                                                                                                                                                            
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    false,                                                                                                                                                               
    false,                                                                                                                                                               
    false ],                                                                                                                                                             
    SE: [ false, false, false ] } ));

    或者:

    console.log(JSON.stringify([{ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ]},                                                                                        
    {LU: [ true,                                                                                                                                                            
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    true,                                                                                                                                                                
    false,                                                                                                                                                               
    false,                                                                                                                                                               
    false ]},                                                                                                                                                             
    {SE: [ false, false, false ] }] ));

    您的 JSON 格式不正确。

    使用类似的东西

    jsonformatter.curiousconcept.com

    jsoneditoronline.org

    验证您的 JSON

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多