[英]Parsing with json.parse
i'm trying to use javascript/json.parse to parse some information in json. 我正在尝试使用javascript / json.parse解析json中的一些信息。
here is an example code in which i am using it. 这是我正在使用的示例代码。
var jsontext = '{"place": Jamaica}';
var countries = JSON.parse(jsontext);
document.write(countries.place);
That format does not work. 该格式不起作用。
if i manually alter the json text to 如果我手动将json文本更改为
var jsontext = '{"place": "Jamaica"}';
var countries = JSON.parse(jsontext);
document.write(countries.place);
I get the result Jamaica which works. 我得到了牙买加有效的结果。 unfortunately the way i get the information does not have the quotation marks around Jamaica . 不幸的是,我获取信息的方式在牙买加周围没有引号。
JS Fiddle http://jsfiddle.net/k3V9p/ JS小提琴http://jsfiddle.net/k3V9p/
If you need to do this a lot how about a function to wrap the text in quotes. 如果您需要大量执行此操作,那么将文本用引号引起来的函数怎么样。
function wrapInQuotes (str) {
return '"' + str + '"';
}
If you need it inline: 如果您需要内联:
(function (str) {return '"' + str + '"';}('string'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.