javascript - JSON.stringify = illegal access -


i have array objects in javascript. want save array .json file.

before added objects file, console.log objects.

// client object {id: "1", color: "#00ff00"} object {id: "2", color: "#ff7645"} object {id: "3", color: "#ff8845"} 

then post jsonarray nodejs server this:

// client $.post('/savejson', 'json=' + json.stringify(jsonarray)) // works 

catch post , save file in nodejs 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) }) 

now have json file array objects this:

something.json

[     {"id":"1","color":"#00ff00"},     {"id":"2","color":"#ff7645"},     {"id":"3","color":"#ff8845"} ] 

i read file this:

// nodejs server jsonfile = fs.readfilesync(url, 'utf-8') // output jsonfile:  [{"id":"1","color":"#00ff00"},{"id":"2","color":"#ff7645"},{"id":"3","#ff8845"}] 

parse it

// nodejs server jsonarray = json.parse(jsonfile) // output jsonarray:  [{id: '1',color: '#00ff00'},{ id: '2',color: '#ff7645'},{ id: '3',color: '#ff8845'}] 

and send client

// nodejs server window.newjson(jsonarray) 

at client catch 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]  

and each object console.log object.

output

// client {id: "1", color: "#00ff00"} {id: "2", color: "#ff7645"} {id: "3", color: "#ff8845"} 

noticed object word difference.

now want same file saved again this:

// client $.post('/savejson', 'json=' + json.stringify(jsonarray)) // not working anymore... 

when use json.stringify(jsonarray) @ client side, error: uncaught illegal access

i tried use json.parse(jsonarray) @ client side, 1 give me error uncaught syntaxerror: unexpected token o

when log jsonarray before 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 wrong? why i'm missing object word?

you have stringify jsonarray before sending client side.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -