var moviereviewtext = "{"title": "Friday the 13th", "year": 1980, "reviews": [{"reviewer": "Pam", "stars": 3, "text": "Pretty good, but could have used more Jason"}, {"reviewer": "Alice", "stars": 4, "text": "The end was good, but a little unsettling"}]}";
var jsonobj = eval("(" + moviereviewtext + ")");
在上面显示的变量中,我们以json格式键入数据。但是我需要的是,我有api的url,我必须从那里获取json并将其分配给变量。怎么做?
最佳答案
var moviereviewtext = "{\"title\": \"Friday the 13th\", \"year\": 1980, \"reviews\": [{\"reviewer\": \"Pam\", \"stars\": 3, \"text\": \"Pretty good, but could have used more Jason\"}, {\"reviewer\": \"Alice\", \"stars\": 4, \"text\": \"The end was good, but a little unsettling\"}]}";
responseJSON = JSON.parse(moviereviewtext);
alert(responseJSON.title);
您追求的是JSON.parse()吗?
上面的代码将显示移动的标题,其余数据可以使用点符号以相同的方式进行访问。
此外,我相信您忘记了转义moviereviewtext变量。