Good evening! I need to turn an array in which there are numbers, objects and arrays with objects into a string and back to javascript. (no servers, php, etc.) An example of an object in my array:

{ file : "img/pekka.png", x : random(100,900), y : random(100,500), w : 60, h : 60, angle : 0, alpha : 1, visible : true } 

I tried to do this using JSON.stringify() и JSON.parse() , but gave an error that it could not be parsed. If you do nothing, you get this:

 7,10,1,3.3666666666666596,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],0,8,10,1,5.616666666666652,[object Object],[object Object],[object Object],0 

I would be very grateful for the help! Issued an error: unexpected token f in JSON at position 1003

Closed due to the fact that off-topic participants Grundy , user194374, cheops , sercxjo , Regent 16 Jul '16 at 19:40 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Grundy, Community Spirit, cheops, sercxjo, Regent
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • JSON.stringify is exactly what it does, and in principle the only time it gives an error is that there are circular references in the objects - Grundy
  • 2
    Add to the question an example of an attempt to use these functions, and the data on which errors were issued - Grundy
  • ok, now add - ZeroBone
  • This error JSON.stringify issued? - sercxjo
  • one
    The error is good, but we also need the code, during the execution of which this error occurs - Grundy

1 answer 1

Everything works fine, here's the code I checked

 'use strict'; var obj = { file : "img/pekka.png", x : random(100, 900), y : random(100, 500), w : 60, h : 60, angle : 0, alpha : 1, visible : true }; var str = JSON.stringify(obj); console.log(typeof str); console.log(str); var strToObj = JSON.parse(str); console.dir(typeof strToObj); console.dir(strToObj); function random(x, y) { return x++, y++; }