JSON.stringify =非法访问 [英] JSON.stringify = illegal access
问题描述
我在Javascript中有一个包含对象的数组。我想将此数组保存到 .json
文件。
I have an array with objects in Javascript. I want to save this array to a .json
file.
在我将对象添加到文件之前,我 console.log
对象。
Before I added the objects to the file, I console.log
the objects.
// Client
Object {id: "1", color: "#00FF00"}
Object {id: "2", color: "#FF7645"}
Object {id: "3", color: "#FF8845"}
然后我将jsonArray发布到我的nodejs服务器上,如下所示:
Then I post the jsonArray to my nodejs server like this:
// Client
$.post('/savejson', 'json=' + JSON.stringify(jsonArray)) // This works
抓住帖子并将文件保存在nodejs中,如下所示:
Catch the post and save the file in nodejs like this:
app.router.post('/savejson', function(data) {
url = '/jsonfiles/something.json'
// Nodejs Server
fs.writeFile(url, data.body.json, function(error) {
if(error) {
console.log(error)
return
}
console.log('Saved file: ' + url)
})
现在我有一个带有如下对象的数组的json文件:
Now I have a json file with an array with objects like this:
[
{"id":"1","color":"#00FF00"},
{"id":"2","color":"#FF7645"},
{"id":"3","color":"#FF8845"}
]
我读这样的文件:
// Nodejs Server
jsonfile = fs.readFileSync(url, 'UTF-8')
// Output jsonfile:
[{"id":"1","color":"#00FF00"},{"id":"2","color":"#FF7645"},{"id":"3","#FF8845"}]
解析它
// Nodejs Server
jsonArray = JSON.parse(jsonfile)
// Output jsonArray:
[{id: '1',color: '#00FF00'},{ id: '2',color: '#FF7645'},{ id: '3',color: '#FF8845'}]
并发送回客户端
// Nodejs Server
window.newjson(jsonArray)
在我的客户端,我抓住了以下文件:
At my client I catch the file with:
// Client
window.newjson = function(jsonArray) {
// Here foreach loop
}
// Output jsonArray:
undefined[3]
0:
color: "#00FF00"
id: "1"
__proto__:
1:
color: "#FF7645"
id: "2"
__proto__:
2:
color: "#FF8845"
id: "3"
__proto__:
length: 3
__proto__: undefined[0]
对于每个对象,我 console.log
对象。
And for each object I console.log
the object.
// Client
{id: "1", color: "#00FF00"}
{id: "2", color: "#FF7645"}
{id: "3", color: "#FF8845"}
注意到对象字是不同的。
Noticed the Object word is difference.
现在我希望再次保存相同的文件:
Now I want the same file to be saved again like this:
// Client
$.post('/savejson', 'json=' + JSON.stringify(jsonArray)) // Not working anymore...
当我使用 JSON.stringify(jsonArray)
在客户端,我收到错误:未捕获的非法访问
When I use JSON.stringify(jsonArray)
at client side, I get the error: Uncaught illegal access
我也尝试过使用 JSON.parse(jsonArray)
在客户端,但是这个给了我错误未捕获的SyntaxError:意外的令牌o
I also tried to use JSON.parse(jsonArray)
at client side, but this one give me the error Uncaught SyntaxError: Unexpected token o
当我在第二篇文章之前登录jsonArray时:
When I log the jsonArray BEFORE the second post:
// 1
console.log(jsonArray)
// Result
Array[3]
0:
color: "#00FF00"
id: "1"
__proto__:
1:
color: "#FF7645"
id: "2"
__proto__:
2:
color: "#FF8845"
id: "3"
__proto__:
length: 3
__proto__: Array[0]
// 2
console.log(jsonArray.toString())
// Result
[object Object],[object Object],[object Object]
// 3
JSON.stringify(jsonArray)
// Result
Uncaught illegal access
// 4
JSON.parse(jsonArray)
// Result
Uncaught SyntaxError: Unexpected token o
我错了什么?为什么我错过对象
字?
What did I wrong? Why I'm missing the Object
word?
推荐答案
你必须stringify jsonArray
然后再发送回客户端。
You have to stringify jsonArray
before sending it back to the client side.
这篇关于JSON.stringify =非法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!