How can I convert a string that describes an object into a JSON string using JavaScript (or jQuery)? e.g: Convert this (NOT a valid JSON string): var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }" into this: str = '{ "hello": "world", "places": ["Africa", "America", "Asia", "Australia"] }' I would love to avoid using eval() if possible.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015. Learn moreSee full compatibilityReport feedback The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer a
console.log(JSON.stringify({ x: 5, y: 6 })); // Expected output: '{"x":5,"y":6}' console.log( JSON.stringify([new Number(3), new String("false"), new Boolean(false)]), ); // Expected output: '[3,"false",false]' console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] })); // Expected output: '{"x":[10,null,null,null]}' console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く