QuantumArchi
QuantumArchi

Reputation: 73

correctly adding to json from javascript

hi im having trouble correctly adding to my json

here is the code.

When i console.log the string im trying to add is

     {"type":"#","name":"wh2xogvi","list":[{"0":"background-color"},{"1":"border"},{"2":"width"},      {"3":"height"},{"4":"margin"}],"listvalues":[{"0":"#aaa"},{"1":"2px solid #000"},{"2":"1040px"},{"3":"50px"},{"4":"0 auto"}]}           

it is valid json

     var jsonltoload = JSON.stringify(eval("(" + jsonloadtostring + ")"));
     console.log(jsonltoload); // this is the console log i was talking about higher up
     fullJSON.styles.objectcss.push(jsonltoload);    

But when i actually look at the json it is wrong ends up something like this

       "{\"type\":\"#\",\"name\":\"unkd42t9\",\"list\":[{\"0\":\"background-color\"},{\"1\":\"border\"},{\"2\":\"width\"},{\"3\":\"height\"},{\"4\":\"clear\"}],\"listvalues\":[{\"0\":\"#ddd\"},{\"1\":\"2px solid #000\"},{\"2\":\"100%\"},{\"3\":\"50px\"},{\"4\":\"both\"}]}",              

the fullJSON comes from JSON.parse(json); which comes from a file

Upvotes: 0

Views: 41

Answers (2)

Saravanan
Saravanan

Reputation: 293

I think the JSON string is trying to escape the double quotes character you have added to string, resulting in the string. try to enclose the whole string with single quotes rather than double quotes

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816374

You seem to confuse JSON, a textual, language-independent data representation, with JavaScript objects, a language-specific data type.

JSON.stringify returns a string (containing JSON), so jsonltoload is a string. I guess you simply want to parse the JSON and add the resulting object:

var obj = JSON.parse(jsonloadtostring);
fullJSON.styles.objectcss.push(obj);    

Upvotes: 2

Related Questions