LoginSignup
32
27

More than 5 years have passed since last update.

【jQuery】json形式の文字列をオブジェクトへ変換

Last updated at Posted at 2015-02-18

jQueryでどうやる?

var obj = $.parseJSON('{"name":"John"}');

不適切なjson文字列が指定された場合、このメソッドは例外をスローします。

{test: 1}
ダブルクォートで囲まれていない。
※というかjsonってキーも囲むんだね。。

{'test': 1}
ダブルクォートではなくシングルクォートを使用している。
※というかjsonってシングルクォートダメなんだね。。

参考:$.parseJSON()

javascriptでどうやる?

var data = '{a:123,b:"hogehoge"}';
var obj = (new Function("return " + data))();

なんかすごい。

参考:JSONらしき文字列をオブジェクトに変換するJavaScript

32
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
27