【问题标题】:Nested array loses information when variable declared声明变量时嵌套数组丢失信息
【发布时间】:2016-08-10 23:40:41
【问题描述】:

我在 json 中有一个数组,由其他三个数组组成。这三个数组之一还嵌套了另一个数组,并且该数组还嵌套了第三个数组:

{
    "items": [
        {location: "Tiberius' Palace", 
         starting_line: 0, 
         line_text: "I command sylyns, in þe peyn of forfetur,", 
         duration: 1, 
         characters: {first_character: 
                          {character: "Emperor", color: "660066"}
                     }
        }
    ],
    "directions": [],
    "locations": []
}

我以非常标准的方式将此 json 输入 d3:

d3.json("MM_chart_test.json", function(error, json) 

然后调用三个数组中的每一个:

       var items = json.items;

       var locations = json.locations;

       var directions = json.stage_directions;

我遇到的问题是,进行这些变量声明会导致 items 变量丢弃比字符声明更深的任何内容,因此 json 看起来像这样:

{location: "Tiberius' Palace", 
 starting_line: 0, 
 line_text: "I command sylyns, in þe peyn of forfetur,", 
 duration: 1, 
 characters: {first_character: 
              null}
}

除了变量声明之外,我没有做任何事情,其他两个数组都可以正常工作。对我来说很明显问题在于声明变量,但我想知道最好的方法是在不丢失两层深度信息的情况下这样做。谢谢。

【问题讨论】:

  • 如果您console.log(json.items),则填充 first_character 属性?
  • 您的 json 无效。复制粘贴到jsonformatter.curiousconcept.com
  • @jeesh 不,那个

标签: javascript arrays json d3.js


【解决方案1】:

在属性名称周围添加引号。正如上面Sebastian Lorenzo 所说,它是无效的JSON。

{  
   "location":"Tiberius' Palace",
   "starting_line":0,
   "line_text":"I command sylyns, in þe peyn of forfetur,",
   "duration":1,
   "characters":{  
   "first_character":null
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多