Javascript 在PaperJS中加载外部JSON文件
我想这真的很简单:Javascript 在PaperJS中加载外部JSON文件,javascript,json,paperjs,Javascript,Json,Paperjs,我想这真的很简单: project.importJSON('structures.json') 不幸的是,这给了我: JSON解析错误:意外的标识符“结构” 此“structures”必须来自JSON文件structures.JSON本身,如下所示: { “结构”:[ {“名称”:“棚”,“宽”:4,“高”:3,“深”:5}, {“名称”:“房子”,“宽度”:6,“高度”:7,“深度”:5}, {“名称”:“工厂”,“宽度”:4,“高度”:3.5,“深度”:6.5, “深度1”:5.5, “深度
project.importJSON('structures.json')
不幸的是,这给了我:
JSON解析错误:意外的标识符“结构”
此“structures”
必须来自JSON文件structures.JSON
本身,如下所示:
{
“结构”:[
{“名称”:“棚”,“宽”:4,“高”:3,“深”:5},
{“名称”:“房子”,“宽度”:6,“高度”:7,“深度”:5},
{“名称”:“工厂”,“宽度”:4,“高度”:3.5,“深度”:6.5,
“深度1”:5.5,
“深度2”:4.5}
]
}
你有什么想法可以让它工作吗?我不想为此使用jQuery
编辑
因此,在我看来,只有当JSON是实际的纸质对象时,使用importJSON才能起作用。同样的错误不断发生
替代方案是什么?我希望我的外部“结构”数据基本上是一个JavaScript文件。我是否应该使用常规路线进行此操作
我的html如下所示:
您应该对数据进行字符串化。 如果json数据位于不同的文件中,则应首先读取该文件 这是您读取文件的方式 同步 异步
请注意,执行此操作时,不需要对数据进行字符串化,因为您将其作为文本读取,因此只需将
client.responseText
内容传递给importJSON
函数是的,如何执行?它在同一个文件夹中,什么是优雅的解决方案?@Code MonKy我在编辑中解释得更好,希望它能帮助汉克斯,你真的帮了我很多。
var structures = {
"structures": [
{"name":"shed", "width": 4, "height": 3, "depth": 5},
{"name": "house", "width": 6, "height": 7, "depth": 5},
{"name": "factory", "width": 4, "height": 3.5, "depth": 6.5,
"depth_sub_1": 5.5,
"depth_sub_2": 4.5 }
]
}
project.importJSON(JSON.stringify(structures))
var client = new XMLHttpRequest();
client.open('GET', 'path/to/your/file', false);
client.onreadystatechange = function() {
//your data is in client.responseText
console.log(client.responseText);
}
client.send(null);
var client = new XMLHttpRequest();
client.open('GET', 'path/to/your/file');
client.onreadystatechange = function() {
//your data is in client.responseText
console.log(client.responseText);
}
client.send();