Raj Yadav Raj Yadav - 2 months ago 7
Javascript Question

Json in javascript

i am getting a problem to get json string creating in javascript,
in console log getting pure json string

like

[{"elementId":"selectProduct","elemnetValue":"Y"},{"elementId":"productId","elemnetValue":"415"}]


but when i am storing the output of the json in variable

it change like

[{\"elementId\":\"selectProduct\",\"elemnetValue\":\"Y\"},{\"elementId\":\"productId\",\"elemnetValue\":\"415
"}]


so it can't parse by

JSON.parse(jsonString);

Answer

You can use JSON.stringify to get a "parseable" string..

like

var str = JSON.stringify([{"elementId":"selectProduct","elemnetValue":"Y"},{"elementId":"productId","elemnetValue":"415"}]);

var json = JSON.parse(str);

enter image description here

Comments